October 2017
Intermediate to advanced
210 pages
5h 32m
English
Type casting is a way to check the type of an instance and/or to treat the instance as a specified type. In Swift, we use the is keyword to check whether an instance is of a specific type and the as keyword to treat an instance as a specific type.
The following example shows how we would use the is keyword:
if person is SwiftProgrammer {
print("(person.firstName) is a Swift Programmer")
}
In this example, the conditional statement returns true if the Person instance is of the SwiftProgrammer type or false if it isn't. We can use the where statement in combination with the is keyword to filter an array to only return instances of a specific type. In the next example, we filter an array that contains instances of ...
Read now
Unlock full access