如何在Swift 3中转换++或- [英] How to convert ++ or -- in Swift 3?

查看:268
本文介绍了如何在Swift 3中转换++或-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下代码,我试图将其转换为Swift 3,并收到此奇怪的错误无法将Bool类型的值转换为预期的参数类型Int".当我摆脱"++"时,就会出现问题.我也链接到我想完全转换的堆栈溢出问题.谢谢!这是之前的代码以及我尝试转换为的代码:

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:

以前的代码

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

playTrack()

}

转换后的代码

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

}

我想转换为Swift 3的原始问题

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

推荐答案

您想要的恕我直言

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

    playTrack()
}

按需正确替换POSTFIX(没用)

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

这篇关于如何在Swift 3中转换++或-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆