swift一元运算符' ++'不能应用于' Int!'

时间:2015-08-10 22:49:12

标签: swift xcode6 operators unary-operator

在“基本操作符”一节中,“Swift编程语言”指南指出++是一个有效的运算符:

  

“更复杂的例子包括逻辑AND运算符&& (如果是   进入DoodeCode&& passRetinaScan)和增量运算符++ i,   这是将i的值增加1的快捷方式。“摘录自:   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一元运算符' ++'不能应用于类型的操作数   '诠释'!

为什么呢? Thnx为您提供所有帮助

3 个答案:

答案 0 :(得分:9)

你应该先解开它

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

  

隐式展开的可选项是一个正常的可选项   场景

如果您使用带有可选

的一元运算符,您将收到相同的错误
var a : Int? = 12
a++ //Unary operator '++' cannot be applied to an operand of type 'Int?'

答案 1 :(得分:3)

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

所以你只需要使用这个

如果你用来递增

  

i = +1而不是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])
}

答案 2 :(得分:1)

删除!

末尾的var availableNodesNum: Int!