Protocol and OCprotocol in OC
1. Simple use 1. Basic use
- Can be used to declare a lot of methods (member variables cannot be declared)
- As long as a class complies with this Protocol, it is equivalent to having all the method declarations in this Protocol.
- As long as the parent class complies with a certain protocol, it is equivalent to the sub-class also complies
2. Format
@ Protocol name
// Method declaration list
@ End
- A class complies with the Protocol
@ Interface Class Name: parent class <protocol name>
@ End
3. Keywords
The Protocol has two keywords to control whether a method is implemented (@ required by default). In most cases, it is used for communication between programmers.
- @ Required: This method must be implemented (if not implemented, the compiler will issue a warning)
- @ Optional: This method does not have to be implemented
4. Agreement compliance
- One protocol can comply with multiple other protocols. Multiple protocols are separated by commas (,).
- An agreement that complies with other protocols is equivalent to having a method statement in other Protocols
@ Protocol name <protocol 1, protocol 2>
@ End
5. Base protocols
- NSObject is a base class, the most fundamental and basic class. Any other class will eventually inherit it.
- In fact, there is also a protocol named NSObject, which is a base protocol and the most fundamental and basic protocol.
- The NSObject Protocol declares many basic methods, such as description, retain, and release.
- We recommend that each new protocol comply with the NSObject protocol.
6. Specify the protocol when defining variables.
// NSObject-type object, which must comply with the NSCopying Protocol
NSObject <NSCopying> * obj;
// Any OC object and comply with the NSCoding Protocol
Id <NSCoding> obj2;
Ii. Proxy design mode 1. Design Principle
- If you don't want to do something troublesome, you can ask someone for help, that is, hand it over to the proxy object.
2. Design Principles
- First, you must have a proxy object property.
- Next, we need to clearly know the proxy methods.
- Finally, ensure decoupling.
3. Implementation Scheme
- Define a protocol and declare some methods to communicate with the proxy.
- Has a proxy property id <protocol> delegate
- Allow the proxy to comply with protocol