Protocol (Protocol)
Protocol is common in iOS, the protocol is a set of standards, similar to a list, it says that there are some things to be done, it depends on who signed the agreement, if who signed the agreement, then it will achieve (complete) the agreement required to complete the things.
Create Protocol,command+n, select Objective-c file, then write the protocol name in file, file type Select protocol. You can generate a. h file.
All protocols are declared in this. h file.
The declaration of the Agreement begins with @protocol, ends with @end, and puts in place some methods that need to be implemented, which are decorated with @required (which must be implemented) and @optional (which can be implemented without implementation).
@protocol Agreement name <nsobject>,nsobject in the <> number, it is no longer a class, but a protocol.
An example:
MyProtocol.h (Write protocol)
#import <Foundation/Foundation.h> Claim Agreement @protocol MyProtocol <nsobject>//in <> NSObject is not a class, but a protocol @required//requirements are required. Must implement -(void) eating; -(void) drinking; @optional//Optional. An optional implementation -(void) shopping; @end |
Student.h (signed agreement)
#import <Foundation/Foundation.h> #import "MyProtocol.h"//Introduction protocol @interface student:nsobject<myprotocol>//Signing Agreement @end |
STUDENT.M (Implementation protocol content)
#import "Student.h" @implementation Student -(void) eating{ } -(void) drinking{ } -(void) shopping{ } @end |
MAIN.M Execution Protocol
#import <Foundation/Foundation.h> #import "Student.h" int main (int argc, const char * argv[]) { @autoreleasepool { Student *s1 = [[Student alloc]init]; [S1 eating]; [S1 drinking]; [S1 shopping]; } return 0; } |
Protocol (Protocol) (20)