Type casting with protocols

Type casting is a way to check the type of the instance and/or to treat the instance as a specified type. In Swift, we use the is keyword to check whether an instance is a specific type and the as keyword to treat the instance as a specific type.

To start with, let's see how we would check the instance type using the is keyword. The following example shows how this is done:

for person in people { 
  if person is SwiftProgrammer { 
    print("(person.firstName) is a Swift Programmer") 
  } 
} 

In this example, we use the if conditional statement to check whether each element in the people array is an instance of the SwiftProgrammer type and, if so, we print that the person is a Swift programmer to the console. While this is a good method to check whether we have an instance of a specific class or structure, it is not very efficient if we want to check for multiple types. It would be more efficient to use a switch statement, as shown in the next example:

for person in people { 
  switch person { 
    case is SwiftProgrammer: 
      print("(person.firstName) is a Swift Programmer") 
    case is FootballPlayer: 
      print("(person.firstName) is a Football Player") 
    default: 
      print("(person.firstName) is an unknown type") 
  } 
} 

In the previous example, we showed how to use the switch statement to check the instance type for each element of the array. To do this check, we use the is keyword in each of the case statements in an attempt to match the instance type.

In Chapter 4, Control Flow and Functions, we saw how to filter conditional statements with the where statement. We can also use the where statement with the is keyword to filter the array, as shown in the following example:

for person in people where person is SwiftProgrammer { 
  print("(person.firstName) is a Swift Programmer") 
} 

Now let's look at how we can cast an instance of a class or structure to a specific type. To do this, we would use the as keyword. Since the cast can fail if the instance is not of the specified type, the as keyword comes in two forms: as? and as!. With the as? form, if the casting fails, it returns a nil, and with the as! form, if the casting fails, we get a runtime error; therefore it is recommended to use the as? form unless we are absolutely sure of the instance type or we perform a check of the instance type prior to doing the cast.

While we do show examples of typecasting with as! in this book, so you are aware that it is there, we highly recommend that you do not use it in your projects because it can cause a runtime error.

Let's look at how we would use the as? keyword to cast an instance of a class or structure to a specified type:

for person in people { 
  if let p = person as? SwiftProgrammer { 
    print("(person.firstName) is a Swift Programmer") 
  } 
} 

Since the as? keyword returns an optional, we use optional binding to perform the cast, as shown in this example.

Now that we have covered the basics of protocols, let's dive into one of the most exciting features of Swift: protocol extensions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset