Tho*_*ira 3 operators unary-operator swift xcode6
在Basic Operators一节中,Swift编程语言指南指出++是一个有效的运算符:
"更复杂的例子包括逻辑AND运算符&&(如在输入DoorCode &&传递瑞典扫描中)和增量运算符++ i,它是将i的值增加1的快捷方式."摘录自:Apple Inc."Swift编程语言."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++
Run Code Online (Sandbox Code Playgroud)
构建错误显示:
swift一元运算符'++'不能应用于'Int!'类型的操作数
为什么?Thnx为您提供所有帮助
iel*_*ani 10
你应该先解开它
availableNodesNum!++
Run Code Online (Sandbox Code Playgroud)
因为在标准库++中仅将非选项定义为前缀和后缀运算符.
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
Run Code Online (Sandbox Code Playgroud)
请记住,根据文件:
隐式展开的可选项是幕后的常规可选项
如果您使用带有可选的一元运算符,您将得到相同的错误
var a : Int? = 12
a++ //Unary operator '++' cannot be applied to an operand of type 'Int?'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6418 次 |
| 最近记录: |