O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Protocol inheritance

Protocol inheritance is where one protocol can inherit the requirements from one or more protocols. This is similar to class inheritance in OOP, but instead of inheriting functionality, we are inheriting requirements. We can also inherit requirements from multiple protocols, whereas a class in Swift can have only one superclass. Let's start off by defining four protocols, named Name, Age, Fur, and Hair:

protocol Name {   var firstName: String { get set }  var lastName: String { get set } 
} 
 
protocol Age { 
  var age: Double { get set } 
} 
 
protocol Fur { 
  var furColor: String { get set } 
} 
 
protocol Hair { 
  var hairColor: String { get set } 
} 

Each of the four protocols has different requirements.

There is one thing that I would ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required