加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 一元运算符不能应用于Int类型的操作数

发布时间:2020-12-14 02:29:04 所属栏目:百科 来源:网络整理
导读:为什么下面的快速代码给我带来错误“一元运算符”不能应用于’Int’类型的操作数”??? (在 Xcode-6.3.2上使用swift-1.2) struct Set { var player1Games: Int var player2Games: Int init() { self.player1Games = 0 self.player2Games = 0 } func increaseP
为什么下面的快速代码给我带来错误“一元运算符”不能应用于’Int’类型的操作数”??? (在 Xcode-6.3.2上使用swift-1.2)
struct Set {

    var player1Games: Int
    var player2Games: Int

    init() {
        self.player1Games = 0
        self.player2Games = 0
    }

    func increasePlayer1GameScore () {
        player1Games++   // error: Unary operator '++' cannot be applied to an operand of type 'Int'
    }

    func increasePlayer2GameScore () {
        player2Games++   // error: Unary operator '++' cannot be applied to an operand of type 'Int'
    }

}
错误消息有点误导.你需要做的是在func之前添加变异来指定它将结构为 modify:
struct MySet {

    var player1Games: Int
    var player2Games: Int

    init() {
        self.player1Games = 0
        self.player2Games = 0
    }

    mutating func increasePlayer1GameScore() {
        player1Games++
    }

    mutating func increasePlayer2GameScore() {
        player2Games++
    }

}

注意:Set是Swift中的一个类型,我建议为你的struct使用不同的名称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读