Reputation: 239
In the section Basic Operators, the Swift Programming Language guide states that ++ is a valid operator:
“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
However, when attempting this in a playground:
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++
A build error shows:
swift Unary operator '++' cannot be applied to an operand of type 'Int!'
Why? Thnx for all your help
Upvotes: 3
Views: 6875
Reputation: 3907
Unary operator '++' cannot be applied to an operand of type '@lvalue Int'
So You just use this
if you use to increment then
i = +1 instead of i++
if you use in loop then try this
for i in 0..<xList.count{
print(i) // for int
print(xList[i]) // for value
}
instead of
for var i = 0; i<xList.count; i++{
print(i)
print(xList[i])
}
Upvotes: 3
Reputation: 18581
you should unwrap it first
availableNodesNum!++
because in the standard library ++
is only defined for non-optionals as a prefix and postfix operator.
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
& bear in mind that according to the documentation :
An implicitly unwrapped optional is a normal optional behind the scenes
You'll get the same error if you use unary operator with an optional
var a : Int? = 12
a++ //Unary operator '++' cannot be applied to an operand of type 'Int?'
Upvotes: 11