Junior — Middle
Can you give an example of a situation where the protocol principle is applied?
sobes.tech AI
Answer from AI
The protocol principle in iOS is used when you need to define a set of requirements (methods, properties) that a class or structure must implement without specifying the exact implementation.
For example, if you have different types of objects that need to respond to a button press, you can create a protocol:
protocol ButtonActionDelegate {
func didTapButton()
}
class ViewController: UIViewController, ButtonActionDelegate {
func didTapButton() {
print("Button tapped")
}
}
This approach allows for writing flexible and extensible code, where different objects can implement the same protocol in their own way.