Now, it is time to code the protocols in Swift. We will code the following five protocols:
ComicCharacter
GameCharacter
Alien
Wizard
Knight
The following UML diagram shows the five protocols that we will code in Swift with their required properties and methods included in the diagram:
The following lines show the code for the ComicCharacter
protocol. The public
modifier followed by the protocol
keyword and the protocol name, ComicCharacter
, composes the protocol declaration. As it happens with class declarations, the protocol body is enclosed in curly brackets ({}
):
public protocol ComicCharacter { var nickName: String { get set } func drawSpeechBalloon(message: String) func drawSpeechBalloon(destination: ComicCharacter, message: String) func drawThoughtBalloon(message: String) }
The protocols declare a nickName
read/write String
stored property requirement, a drawSpeechBaloon
method requirement overloaded twice, and a drawThoughtBalloon
method requirement...