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

Declaring a class that works with two constrained generic types

Now, it is time to code another protocol that will be used as a constraint later, when we define another class that takes advantage of generics with two constrained generic types. The following lines show the code for the DeeJayProtocol protocol. The public modifier followed by the protocol keyword and the protocol name, DeeJayProtocol, composes the protocol declaration, as follows. The code file for the sample is included in the swift_3_oop_chapter_06_10 folder:

    public protocol DeeJayProtocol { 
      var name: String { get } 
     
      init(name: String) 
     
      func playMusicToDance() 
      func playMusicToSing() 
    } 

The protocol declares a name: String read-only stored property and two method requirements:

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