------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
- Can be used to declare a whole bunch of methods (cannot declare member variables)
- As long as a class adheres to this protocol, it is equivalent to having all the method declarations in this agreement
- As long as the parent class adheres to a protocol, the subclass also adheres to the
Define a protocol called MyProtocol.MyProtocol.h@protocol myprotocol <NSObject> @required//requirements implementation, do not know the idle will issue a warning, if nothing is written, the default is @required-(void) test;-(void) test2; @optional//Do not require implementation-(void) test3; @endPerson.h@protocol Advance Declaration of the myprotocol;//agreement, similar to @class@interface Person:nsobject <MyProtocol> @endperson.m#import MyProtocol.h;@implementation person-(void) test{}-(void) test2{} @end
- One protocol can comply with multiple protocols, and multiple protocols are separated by commas .
- A protocol that complies with other agreements is equivalent to having a method declaration in another agreement
@protocol Agreement name < agreement 1, agreement 2>
@end
1 "Class Compliance Agreement: @interface Class Name: Parent class name < protocol name 1, agreement name 2> @end2 Agreement Compliance Agreement: @protocol Agreement name < Other agreement name 1, other agreement name 2> @end1. Base protocol
- nsobject is a base class, the most fundamental and basic class, and any other class will eventually inherit it
- nsobject It's a base protocol, the most fundamental protocol
- nsobject Many of the most basic methods are stated in the agreement, such as description retain Span style= "font-family: the song Body;" >, release et
- nsobject
The object required for OBJ3 to be saved must be to comply with MyProtocol this protocol;nsobject<myprotocol> *obj3;id<myprotocol> obj3; The saved object must obey MyProtocol3 and inherit the personperson<myprotocol3> *obj4, and the attributes declared in the @property can also be used as a compliance restriction @property (nonatomic , strong) Class name < protocol name > * attribute name, @property (nonatomic,strong) id< protocol name > attribute name; the protocol can be defined in a separate. h file, or in a Class 1 " If this protocol is used only in a class, the protocol should be defined in the Class 2 if the protocol is used in many classes, it should be defined in a separate file that can be defined in separate. h and. m files, or in the original Class 1, which is defined in a separate file 2 as defined in the original class, Only requires to be able to read the grammar #import "TicketDelegate.h" @interface person:nsobject-(void) buyticket;//has a proxy attribute, the proxy class name is random, Must comply with Ticketdeltegate agreement @property (nonatomic,retain) id<ticketdelegate> delegate; @end
Dark Horse Programmer--protocol (protocol)