O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

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

Combining initializer requirements in protocols with generic types

We included an initializer requirement when we declared the AnimalProtocol protocol, so we know the necessary arguments to create an instance of any class that conforms to this protocol. We will add a new method that creates an instance of the generic type AnimalElement and adds it to the party members in the Party<AnimalElement> class.

The following lines show the code for the new createAndAddMember method that receives a name String argument and returns an instance of the generic type AnimalElement. We add the method to the body of the Party<AnimalElement: AnimalProtocol> where AnimalElement: Equatable open class declaration. The code file for the sample is included in the swift_3_oop_chapter_06_07 ...

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