Declaring a protocol to be used as a constraint

We will create an AnimalProtocol protocol to specify the requirements that a type must meet in order to be considered an animal. Then, we will create an Animal base class that conforms to this protocol, and then, we will specialize this class in three subclasses: Dog, Frog, and Lion. Then, we will create a Party class that will be able to work with the instances of any class that conforms to the AnimalProtocol protocol through generics. We will work with a party of dogs, a party of frogs, and a party of lions.

Then, we will create a DeeJayProtocol protocol and generate a HorseDeeJay class that conforms to this new protocol. We will create a subclass of the Party class named PartyWithDeeJay, which will ...

Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.