"Protocol/protocol"
———————————————————————————————————————
<1>. Definition of the Agreement
In OC is the declaration of a canonical method, which can be used to implement a syntax for multiple inheritance or inter-object communication.
<2>. The wording of the Agreement:
@protocol Agreement name <NSObject>
@required (default method)
Methods that must be implemented by a class that conforms to the protocol
@optional
Methods for implementing class-selectable implementations of protocols
@end
Note the protocol has only one header file, which declares the method
<3>. There are two kinds of permissions for the methods declared in the agreement:
@required: (default) required, that is, the class that conforms to the Protocol, must implement this method
@optional: Optional, the class that conforms to the protocol, can implement this method, also can not implement.
<4>. Issues to be aware of when using protocols
1. Only the method is declared in the agreement and the method is not implemented
2. The class that complies with the agreement must import the protocol header file
3. The method of @required permissions in the protocol must be implemented
<5>. Follow the class notation of (conform) protocol
@interface Dog:nsobject <eatprotocol, runprotocol>
The dog class complies with the protocol, and if multiple protocols are followed (multiple inheritance), the protocols are separated by commas
Note the class that complies with the agreement creates an object that conforms to the protocol
<6>. ID pointer notation for holding agreement
@property (weak) id<mydelegate> delegate;
Delegate is the ID pointer that holds the agreement, only the method declared in the protocol can be called
ID pointer that holds the agreement and can only point to objects that comply with the agreement
=================================
"Agent/delegate"
———————————————————————————————————————
<1>. Agent
The important use of the Protocol, one is to standardize the interface, one is to implement the proxy.
A proxy is a mechanism for implementing inter-object communication between two classes.
The "principal" active party holds an ID pointer with the agreement and can use the agreement.
@property (weak) id<mydelegate> delegate;
Proxy objects do not use strong, write weak, represent retain, and directly assign values as normal variables
Reasons to use weak: preventing memory leaks
The "Agent" passive party complies with the Protocol and implements the method in the protocol.
Agent implementation steps:
The first step: make an agreement
Step Two: Client: Use Agreement
Step three: Agent: Implementing the Protocol
Fourth step: Establish agent relationship
<2>. Callback
Another function of the protocol is to make callbacks.
When we need to complete a functional module, but do not know which object to use the function module, through the protocol, you can specify which method of receiving the data to obtain the data, called the process called "callback".
The principal party follows the agreement and implements the method.
The "Agent" passive party holds the ID pointer with the agreement and can use the protocol.
Callback Procedure:
1, the principal party registers the agent object, invokes the agent to realize the function;
2. The agent uses the agreement by holding the ID pointer of the agreement;
3, the principal party to achieve the agreement.
OC_ Protocol Agent