From 9c18774c349cbb0821f59f77c6fff3908139bff7 Mon Sep 17 00:00:00 2001 From: hamidra Date: Fri, 1 Nov 2019 20:49:53 -0700 Subject: add downcast to the tutorial --- swift.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'swift.html.markdown') 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") -- cgit v1.2.3 From 88795664c5ff1980f41bfa39f853cef12f60e92e Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Mon, 2 Dec 2019 16:57:13 -0500 Subject: Return value of `findIndex` is `Optional` --- swift.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'swift.html.markdown') diff --git a/swift.html.markdown b/swift.html.markdown index 1f9fe897..8c1a7755 100644 --- a/swift.html.markdown +++ b/swift.html.markdown @@ -904,7 +904,7 @@ func findIndex(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 { -- cgit v1.2.3