为什么下面的快速代码给我带来错误“一元运算符”不能应用于’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++ } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。