Method requirements

A protocol can require that the conforming class or structure provides certain methods. We define a method within a protocol exactly as we do within a class or structure, except without the curly braces or method body. Let's add a getFullName() method to our FullName protocol and Scientist class.

protocol FullName {
var firstName: String { get set }
var lastName: String { get set }
func getFullName() -> String }

Now, we will need to add a getFullName() method to our Scientist class so that it will conform to the protocol:

class Scientist: FullName {  
  var firstName = "" 
  var lastName = ""  
  var field = "" 
 
  func getFullName() -> String { 
    return "(firstName) (lastName) studies (field)" 
  } 
} 

Structures can conform to Swift protocols exactly as classes do. In fact, the majority of the Swift standard library are structures that implement the various protocols that make up the standard library. The following example shows how we can create a FootballPlayer structure that also conforms to the FullName protocol:

struct FootballPlayer: FullName {  
  var firstName = "" 
  var lastName = ""  
  var number = 0 
 
  func getFullName() -> String { 
    return "(firstName) (lastName) has the number (number)" 
  } 
} 

When a class or structure conforms to a Swift protocol, we can be sure that it has implemented the required properties and methods. This can be very useful when we want to ensure that certain properties or methods are implemented over various classes, as our preceding examples show.

Protocols are also very useful when we want to decouple our code from requiring specific types. The following code shows how we would decouple our code using the FullName protocol, the Scientist class, and the FootballPlayer structure that we have already built:

var scientist = Scientist()  
scientist.firstName = "Kara"  
scientist.lastName = "Hoffman"  
scientist.field = "Physics" 
 
var player = FootballPlayer(); 
 
player.firstName = "Dan"  
player.lastName = "Marino"  
player.number = 13 
 
var person: FullName 
person = scientist 
print(person.getFullName())  
person = player  
print(person.getFullName()) 

In the preceding code, we begin by creating an instance of the Scientist class and the FootballPlayer structure. We then create a person variable that is of the FullName (protocol) type and set it to the scientist instance that we just created. We then call the getFullName() method to retrieve our description. This will print out the Kara Hoffman studies Physics message to the console.

We then set the person variable equal to the player instance and call the getFullName() method again. This will print out the Dan Marino has the number 13 message to the console.

As we can see, the person variable does not care what the actual implementation type is. Since we defined the person variable to be of the FullName type, we can set the variable to an instance of any type that conforms to the FullName protocol. This is called polymorphism. We will cover polymorphism and protocols more in Chapter 6, Using Protocols and Protocol Extensions and Chapter 7, Protocol-Oriented Design.

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

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