The protocol in OC is similar to the excuse of Java
The Protocol is special.ProgramDesign Patterns are used to declare methods implemented by other classes. Protocols are useful in the following scenarios:
1. There must be other class implementation methods.
2. Excuses for declaring unknown classes
3. Communication between two classes
Basic Features of the Protocol
The Protocol can be implemented by any class
The Protocol itself is not a class. It defines an interface that can be implemented by other classes.
Protocols can also be used for categories
Protocol declaration and implementation
@ Required: indicates the method that must be implemented forcibly.
@ Optional: indicates the method that can be implemented selectively.
// Protocol declaration @ protocol firstprotocol <nsobject> // <nsobject> indicates the base protocol. You can leave @ required-(void) requiredmethod; @ optional-(void) optionalmethod; @ end // use this Protocol @ interface person: nsobject <firstprotocol, secondprotocol> @ end @ implementation person-(void) requiredmethod {}