I'm playing with as! and as? in Swift, and according to Apple docs:
The forced form, as!, attempts the downcast and force-unwraps the result as a single compound action.
And here's my code in playground:
let arr:[Any] = [1];
let a = arr.first;
print("a is \(a)");//a is Optional(1)
let x = a as! Int!;
print("x is \(x)");//x is Optional(1)
let y: Int! = a as! Int!;
print("y is \(y)");//y is Optional(1)
var z = 1 + y;// z = 2
var t = 1 + x;// Compile error
The last 2 lines really confused me because x and y are supposed to have the type of (Int!) (according to Apple's doc above). But here only y could be used in the assignment clause (in z), yet x couldn't (in t).
Can someone please explain this?
Please excuse me if this question's been asked somewhere else on SO. Also please don't mind my semicolon(;), it's just a habit so I won't mess up while switching between Objc and Swift :). Edited: Remove the ":" that shouldn't be there. Thanks to Hamish