-------iOS training, Android training, Java training , looking forward to communicating with you! ----------
Protocol protocol
1, Protocol agreement is used to declare the method.
2, as long as a class to comply with a certain agreement, you can have the agreement of all the method statement
3.: Inherit parent class
4. <> Compliance Agreement
5, the protocol can not write implementation and member variables, the suffix of the protocol file is. h (Declaration file).
1. Definition of the Agreement
@protocol Agreement name <NSObject>
Method declaration List ....
@end
For example:
1 #import <Foundation/Foundation.h>23@protocol myprotocol <nsobject > // Declare an agreement which complies with the base agreement 45 -(void) test; 6 7 @end
2. How to comply with the agreement
1> Class Compliance Agreement
@interface Class Name: Parent class name < protocol name 1, protocol name 2>
@end
2> Protocol Compliance Agreement
@protocol Agreement name < Other protocol name 1, other protocol name 2>
@end
3> Base protocol
NSObject is a base class, the most fundamental and basic class, and any other class will eventually inherit it .
There is also a protocol named NSObject, which is a base protocol, the most fundamental and basic protocol
The NSObject protocol declares many basic methods, such as description,retain,release, etc.
It is recommended that each new agreement be subject to the nsobject agreement .
3. Keywords for method declarations in the agreement
1> @required (default) // writing facilitates communication between programmers
Requires implementation, and if not implemented, a warning is issued
2> @optional
Does not require implementation, how can there be no warning
For example:
1 #import<Foundation/Foundation.h>2 3 @protocolMyProtocol <NSObject>4 5- (void) test4;6 7 @required8 9- (void) test;Ten One @optional A -- (void) test3; - the @end
4. When defining a variable, limit the object that this variable holds to a protocol
Class name < protocol name > * variable name;
id< protocol name > variable name;
1 nsobject<myprotocol> *obj; 2 3 ID<MyProtocol> obj2; 4 5 nsobject<myprotocol> *obj = [[Person alloc] init];
Where the person must have complied with the MyProtocol protocol.
If the corresponding protocol is not followed, the compiler warns
The attributes declared in [email protected] can also be used as a limit to comply with the agreement
@property (nonatomic, Strong) class name < protocol name > * attribute name;
@property (nonatomic, strong) id< protocol name > attribute name;
1 @property (nonatomic, Strong) dog<myprotocol> *Dog; 2 3 ID<MyProtocol> dog2;
6. The agreement can be defined in a separate. h file and can also be defined in a class
1> If this protocol is used only in a class, the protocol should be defined in that class
2> If this protocol is used in many classes, it should be defined in a separate file
7. Classifications can be defined in separate. h and. m files and can also be defined in the original class
1> in general, are defined in a separate file
2> defines the classification in the original class, only requires the ability to read the grammar
Protocols are generally used in proxy mode and Observer mode.
Proxy design mode
Design principle
Some troublesome things do not want to do it yourself, you can find someone to help do, that is, to the agent to do
Design principles
You first have to have a proxy object property
Second, it's important to know what the agent means.
Finally, be sure to decouple
Implementation scenarios
Define a protocol protocol name Ticketdelegate, in which you declare some ways to communicate with the agent
has a proxy attribute id<ticketdelegate> delegate
agent's class name whatever, must abide by the ticketdelegate agreement
Note:@property (nonatomic, retain) ID <TicketDelegate> delegate
Let the agent comply with ticketdelegate
Dark Horse programmer-oc-about the Protocol