First, protocol1. Basic use
1> can be used to declare many methods, but cannot be used to declare member variables
2> as long as a class complies with this protocol, it is equivalent to having all the method declarations in this agreement
3> as long as the parent adheres to a protocol, the subclass also adheres to the
2. Format
Preparation of the 1> protocol
@protocol Agreement Name
Method declaration List
-(void) test;
-(void) test2;
+ (void) test;
+ (void) test2;
@end
2> a class to comply with a protocol
@interface Class Name: Parent class < protocol name >
@end
3> when defining a variable, restrict the object that the variable holds to adhere to a protocol
Class name < protocol name > variable name;
For example,:nsobject< protocol name 1> *obj;//now obj can only hold objects that comply with protocol name 1; If you do not follow the compiler will issue a critical warning
The attributes declared in the 4> @property can also be used as a restriction to comply with the protocol
Example: @property (Nonatomic,strong) class name < protocol name > * attribute name;
@property (nonatomic,strong) dog< agreement name > *dog;
3. Keywords
There are two keywords in the protocol that control whether the method is to be implemented (by default, @required), and in most cases the use of communication between programmers
1> @required: This method must be implemented (if not implemented, the compiler warns)
2> @optional: This method is not necessarily implemented
1 @protocolMyProtocol//Claim Agreement2- (void) test;3+ (void) test;4 @optional5- (void) test2;6+ (void) test2;7 @end8 9 #import<Foundation.Foundation.h>Ten One @interfacePerson:nsobject <MyProtocol>//Compliance Agreement A @end - - @implementation the- (void) Test - { - //requires that the method be implemented - } ++ (void) Test - { + //requires that the method be implemented A } at -- (void) Test2 - { - //the method can not be implemented - } -+ (void) Test2 in { - //the method can not be implemented to } + } - @end the * intMain () $ {Panax Notoginseng return 0; -}
Agreement (example)4. Agreement to comply with agreements
1> one protocol can comply with many other protocols, with multiple protocols separated by commas,
2> a protocol complies with other agreements, it is equivalent to having a method declaration in another agreement
3> Multi-protocol format
@protocol Agreement name < Protocol 1, Protocol 2>
@end
5. Base protocol
1> NSObject is a base class, the most fundamental and basic class, and any other class will eventually inherit his
2> NSObject is also an agreement, the most fundamental and basic agreement
Many of the most basic methods are stated in the 3> NSObject protocol, such as description, retain, release, etc.
4> recommends that each new agreement be subject to the NSObject agreement
6. Attention Points
The protocol can be defined in a separate. h file, or it can 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
II. application of the protocol--proxy mode 1. Design principle
Some things do not want to do themselves, you can find a person to help do, that is, to the agent to do
2. Design principles
1> first to have a proxy object property
2> second to understand what the agent has
3> finally to help decouple
3. Implementation scenarios
1> defines a protocol in which to declare some methods of communicating with the agent
2> has a proxy attribute id<protocol>delegate
3> Let Agent obey protocol
1 #import<Foundation.Foundation.h>2 3@property ticketdelegate <NSObjecct>//Design Protocol4- (Double) Ticketprice;//Fares5-(int) Leftticketnumber;//number of votes left6 @end7 8 9 @interfacePerson:nsobjectTen One@propertyID<TIcketDelegate>Delegate; A -- (void) Buyticket; - the @end - - - + @implementation Person - +- (void) Buyticket A { at //I have no time to buy tickets, call Agent - DoublePrice =[_delegate Ticketprice]; - intNumber =[_delegate Leftticketnumber]; -NSLog ("through the agent, the fare =%d, the remaining number of votes is%d", price, number); - } - @end in - to + - @interfaceAgent <TicketDelegate>//Compliance Agreement the @end * $ Panax Notoginseng - @implementationAgent:nsobject the- (Double) Ticketprice + { ARerurn100.0; the } + --(int) Leftticketnumber $ { $ return Ten; - } - @end the - Wuyi the intMain () - { WuPerson *p = [personNew];//people -Agent *a = [AgentNew];//Agent About $P.Delegate= A;//set up agents for people - [P buyticket]; - - return 0; A}
Proxy (example)
OC Language--protocol (protocol)