How to convert ++ or — in Swift 3? [duplicate]

2019-09-21 22:45发布

This question already has an answer here:

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()
}

1条回答
等我变得足够好
2楼-- · 2019-09-21 23:09

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()
}
查看更多
登录 后发表回答