Swift 3.0
Consider a simple concept:
When using if...else in one line, operations must be single, otherwise we need to perform operations under parenthesis to do this as one operation, in our case append(val) is one operation, but myDict[key] = [val] multiple ( myDict[key] is one, and = is one, and [val] is one), so we group them into single ones using parenthesis .
In a simpler way, consider the following arithmatic operations .
//I need 10-5 = 5 let a = 2*4+2-4-3*5 print(a) // -9 //so we can seprate by () let b = ((2*4)+2)-(4-3)*5 print(b) //5
Here we instruct the compiler in an let a way in let a .
Also see
let a:Int? = nil var b:Int? = nil let d = 10 let c = a ?? 10 * b ?? d
Here let c an invalid instruction, there is an error,
The value of the optional type 'Int?' does not unfold; you wanted to use '!' or '?'?
If I force unwrapping optionals a and b , then the error will become,
zero unexpectedly found while expanding optional value
So the constant c becomes,
let c = a ?? 10 * (b ?? d)
That you should use parenthesis around the default value.
myDict[key]?.append(val) ?? (myDict[key] = [val])
RAJAMOHAN-S
source share