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

swift – 一元运算符不能应用于Int类型的 *** 作数,第1张

概述为什么下面的快速代码给我带来错误“一元运算符”不能应用于’Int’类型 *** 作数”??? (在 Xcode-6.3.2上使用swift-1.2) struct Set { var player1Games: Int var player2Games: Int init() { self.player1Games = 0 self.playe 为什么下面的快速代码给我带来错误“一元运算符”不能应用于’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使用不同的名称.

总结

以上是内存溢出为你收集整理的swift – 一元运算符不能应用于Int类型的 *** 作数全部内容,希望文章能够帮你解决swift – 一元运算符不能应用于Int类型的 *** 作数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089945.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存