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

*爱你&永不变心* 提交于 2019-12-25 19:30:15

问题


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:


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


来源:https://stackoverflow.com/questions/42010744/how-to-convert-or-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!