Here are two ways to implement protocol associated types in Swift:
1. Define an associated type in the protocol:
protocol Attackable {
associatedtype Power
func attack() -> Power
}
2. Specify the associated type when conforming to the protocol:
struct Fireball: BasePower {
// implementation
}
class Charizard: Attackable {
typealias Power = Fireball
func attack() -> Fireball {
return Fireball()
}
}
So the protocol defines that some type will be returned from attack(), and the conforming type specifies what that associated type will be when it adopts the protocol.