summaryrefslogtreecommitdiffhomepage
path: root/swift.html.markdown
diff options
context:
space:
mode:
authorDivay Prakash <divayprakash@users.noreply.github.com>2020-02-02 15:13:28 +0530
committerGitHub <noreply@github.com>2020-02-02 15:13:28 +0530
commit8eb11a89a7fc84dde15544c6f7ec99a8f44b0c9f (patch)
tree7605022884b652f7acf879328887a9d191d3ef58 /swift.html.markdown
parentab5a1953e77288ca27bd7b4c931d8bb2e79a9966 (diff)
parentf80889ed47ae42ca4ea2b8118e2b3c48c37eeb9c (diff)
Merge branch 'master' into master
Diffstat (limited to 'swift.html.markdown')
-rw-r--r--swift.html.markdown7
1 files changed, 6 insertions, 1 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 1f9fe897..689c5191 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")
@@ -904,7 +909,7 @@ func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {
}
return nil
}
-findIndex(array: [1, 2, 3, 4], valueToFind: 3) // 2
+findIndex(array: [1, 2, 3, 4], valueToFind: 3) // Optional(2)
// You can extend types with generics as well
extension Array where Array.Element == Int {