mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-28 17:59:05 +03:00
Merge pull request #3755 from hamidra/patch-3
[swift/en-us] add downcast to the tutorial
This commit is contained in:
commit
87669f1c7c
@ -692,6 +692,11 @@ print(mySquare.sideLength) // 4
|
||||
// cast instance
|
||||
let aShape = mySquare as Shape
|
||||
|
||||
// downcast instance:
|
||||
// Because downcasting can fail, the result can be an optional (as?) or an implicitly unwrpped optional (as!).
|
||||
let anOptionalSquare = aShape as? Square // This will return nil if aShape is not a Square
|
||||
let aSquare = aShape as! Square // This will throw a runtime error if aShape is not a Square
|
||||
|
||||
// compare instances, not the same as == which compares objects (equal to)
|
||||
if mySquare === mySquare {
|
||||
print("Yep, it's mySquare")
|
||||
|
Loading…
Reference in New Issue
Block a user