summaryrefslogtreecommitdiffhomepage
path: root/swift.html.markdown
diff options
context:
space:
mode:
authorDivay Prakash <divayprakash@users.noreply.github.com>2020-01-24 20:12:10 +0530
committerGitHub <noreply@github.com>2020-01-24 20:12:10 +0530
commit87669f1c7c40f6ab3ad70bf723cc396979f96306 (patch)
tree05dea704c60d14e3cc7f68a61116b1c3d6456ec4 /swift.html.markdown
parent73338166dfab5e44c67ee6f456811098ed109fb3 (diff)
parent9c18774c349cbb0821f59f77c6fff3908139bff7 (diff)
Merge pull request #3755 from hamidra/patch-3
[swift/en-us] add downcast to the tutorial
Diffstat (limited to 'swift.html.markdown')
-rw-r--r--swift.html.markdown5
1 files changed, 5 insertions, 0 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 1f9fe897..8582131a 100644
--- a/swift.html.markdown
+++ b/swift.html.markdown
@@ -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")