When we run the above program using playground, we get the following result:
res is: 20
res is: 30
res is: 50
Protocol Composition
Swift allows multiple protocols to be called at once with the help of protocol composition.
Syntax
protocol<SomeProtocol, AnotherProtocol>
Example
protocol stname {
var name: String { get }
}
protocol stage {
var age: Int { get }
}
struct Person: stname, stage {
var name: String
var age: Int
}
func print(celebrator: protocol<stname, stage>) {
println("\(celebrator.name) is \(celebrator.age) years old")
}
let studname = Person(name: "Priya", age: 21 )
print(studname)
let stud = Person(name: "Rehan", age: 29 )
print(stud)