swift 一元运算符“++”不能应用于 'Int!' 类型的操作数

swift Unary operator '++' cannot be applied to an operand of type 'Int!'

在基本运算符部分,Swift 编程语言指南指出 ++ 是有效的运算符:

“More complex examples include the logical AND operator && (as in if enteredDoorCode && passedRetinaScan) and the increment operator ++i, which is a shortcut to increase the value of i by 1.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/gb/jEUH0.l

但是,在游乐场尝试此操作时:

class Levels {
    var data = [
        [
            "nodesNum" : 20,
            "lastLevel" : false
        ],
        [
            "nodesNum" : 16,
            "lastLevel" : false
        ],
        [
            "nodesNum" : 13,
            "lastLevel" : false
        ],
        [
            "nodesNum" : 10,
            "lastLevel" : false
        ],
        [
            "nodesNum" : 8,
            "lastLevel" : true
        ]
    ]
}

var levels: Levels!
var availableNodesNum: Int!
var currentLevelData: NSDictionary!
var levelNum:Int = 2

levels = Levels()

currentLevelData = levels.data[levelNum]
availableNodesNum = Int(currentLevelData["nodesNum"]! as! NSNumber)

println(currentLevelData)
println(availableNodesNum)

availableNodesNum++

构建错误显示:

swift Unary operator '++' cannot be applied to an operand of type 'Int!'

为什么?谢谢你的帮助

去掉var availableNodesNum: Int!末尾的!

你应该先打开它

availableNodesNum!++

因为在标准库中 ++ 仅针对非选项定义为前缀和后缀运算符。

prefix public func ++(inout x: UInt8) -> UInt8

prefix public func ++(inout rhs: Float80) -> Float80

postfix public func ++(inout lhs: Double) -> Double

postfix public func ++(inout lhs: Float) -> Float

prefix public func ++(inout rhs: Float) -> Float

postfix public func ++(inout x: Int) -> Int

prefix public func ++(inout x: Int) -> Int

postfix public func ++(inout x: UInt) -> UInt

prefix public func ++(inout x: UInt) -> UInt

/// Replace `i` with its `successor()` and return the original
/// value of `i`.
postfix public func ++<T : _Incrementable>(inout i: T) -> T

postfix public func ++(inout x: Int64) -> Int64

prefix public func ++(inout x: Int64) -> Int64

postfix public func ++(inout x: UInt64) -> UInt64

prefix public func ++(inout x: UInt64) -> UInt64

/// Replace `i` with its `successor()` and return the updated value of
/// `i`.
prefix public func ++<T : _Incrementable>(inout i: T) -> T

postfix public func ++(inout x: Int32) -> Int32

prefix public func ++(inout x: Int32) -> Int32

postfix public func ++(inout x: UInt32) -> UInt32

postfix public func ++(inout lhs: Float80) -> Float80

prefix public func ++(inout x: UInt32) -> UInt32

postfix public func ++(inout x: Int16) -> Int16

prefix public func ++(inout x: Int16) -> Int16

postfix public func ++(inout x: UInt16) -> UInt16

prefix public func ++(inout x: UInt16) -> UInt16

postfix public func ++(inout x: Int8) -> Int8

prefix public func ++(inout x: Int8) -> Int8

postfix public func ++(inout x: UInt8) -> UInt8

prefix public func ++(inout rhs: Double) -> Double

& 切记根据 documentation :

An implicitly unwrapped optional is a normal optional behind the scenes

如果您将一元运算符与可选的

一起使用,您将得到同样的错误
var a : Int? = 12
a++ //Unary operator '++' cannot be applied to an operand of type 'Int?'

一元运算符“++”不能应用于“@lvalue Int”类型的操作数

所以你就用这个

如果你使用递增则

i = +1 instead of i++

如果你在循环中使用,那么试试这个

for i in 0..<xList.count{
print(i) // for int
print(xList[i]) // for value
}

而不是

for var i = 0; i<xList.count; i++{
print(i)
print(xList[i])
}