Junior — Middle
What dispatching method is used to extend protocol capabilities?
sobes.tech AI
Answer from AI
To extend the capabilities of protocols in iOS, protocol extensions are often used. This allows adding implementations of methods and properties to protocols without modifying the classes that implement them.
This method of dispatching can be called static or compile-time dispatch because method calls from protocol extensions are resolved at compile time, not at runtime.
Example:
protocol Greetable {
func greet()
}
extension Greetable {
func greet() {
print("Hello from protocol extension")
}
}
struct Person: Greetable {}
let p = Person()
p.greet() // Outputs: Hello from protocol extension
Thus, dispatching methods from protocol extensions extends functionality without the need to modify the original types.