-3

I have the following code I am trying to convert to Swift 3 and am getting this weird error "Cannot convert value of type Bool to expected argument type Int". The issue arises when I get rid of the "++". I am also linking to the stack overflow question I want to fully convert. Thanks! Here is the previous code and the code I tried to convert to:

Previous code

func previousTrack() {
if currentTrack-- < 0 {
    currentTrack = (playerItems.count - 1) < 0 ? 0 : (playerItems.count - 1)
} else {
    currentTrack--
}

playTrack()

}

Converted code

@IBAction func didTapPreviousButton(_ sender: UIButton) {
    if currentTrack += 1 < 0 {  // Issue occurs here
        currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1)
    } else {
        currentTrack -= 1
    }

    playTrack()

}

Original question I want to convert to Swift 3

EDIT:

@IBAction func didTapPreviousButton(_ sender: UIButton) {
    if (currentTrack - 1) <= 0 {
        currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1)
    } else {
        currentTrack -= 1
    }

    playTrack()

}



@IBAction func didTapNextButton(_ sender: UIButton) {
    if (currentTrack + 1) >= urlPlayerItems.count {
        currentTrack = 0
    } else {
        currentTrack += 1
    }

    playTrack()
}
Community
  • 1
  • 1
user7097242
  • 1,034
  • 3
  • 16
  • 31
  • Spend an extra line, first decrement than check < 0. – vadian Feb 02 '17 at 19:35
  • that'd be my recommendation too – Daij-Djan Feb 02 '17 at 19:36
  • Why are you replacing the old `--` with `+=` ? Why not `-=` ? – rmaddy Feb 02 '17 at 19:36
  • @LeoDabus - that's not a duplicate. The question isn't asking about how to replace `++`. The question is about using `+=` in an `if` statement comparison. – rmaddy Feb 02 '17 at 19:39
  • @rmaddy feel free to reopen if you would like to. Hamish also voted for it to be closed – Leo Dabus Feb 02 '17 at 20:33
  • Ther original code is too complicated. You need only `let previous = (currentTrack - 1 + playerItems.count) % playerItems.count` and `let next = (currentTrack + 1) % playerItems.count`. – Sulthan Feb 02 '17 at 21:59
  • Hey I added a 'edited' version to my question! Please add code snipped showing the correct way to do this. Thanks! – user7097242 Feb 02 '17 at 22:05
  • I just edited my original answer to include a Swift 3 version. If anyone sees any issues, feel free to comment or edit. http://stackoverflow.com/a/35015898/2415822 – JAL Feb 02 '17 at 22:08

1 Answers1

0

What you want IMHO

@IBAction func didTapPreviousButton(_ sender: UIButton) {
    currentTrack -= 1
    if currentTrack < 0 {
        currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1)
    }

    playTrack()
}

Correct replacing of POSTFIX as you have it (useless)

@IBAction func didTapPreviousButton(_ sender: UIButton) {
    if currentTrack < 0 {
        currentTrack -= 1
        currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1)
    }

    playTrack()
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135