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:
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: playMusicToDance
and playMusicToSing
. As you learned in the previous chapter, the protocol includes only the method declaration because the classes that conform to the DeejayProtocol
protocol will be responsible for providing the implementation of the name
stored property and the other two methods.