1. About the Protocol
Protocol is often used in the development of iOS programs using OC. The syntax format of a Protocol is as follows:
@protocol HumanProtocol <NSObject>
@required
- (void)name;
- (void)age;
//@optional
// ...
@end
Copy the code
The concept of a protocol in iOS is similar to that of an interface in Java, which is a bunch of methods declared but not implemented. A class can follow one or more protocols, and any class that follows a protocol owns all of the method declarations in that protocol. Protocol can be defined at the top of a header file of a class and applied directly to that class (for example, when used as a delegate function), or Protocol can be defined individually within a class as an interface that multiple classes follow and implement.
////. H file #import <Foundation/ foundation.h > @protocol HumanProtocol <NSObject> @required - (void)name; - (void)age; //@optional // ... @end @interface Tom : NSObject @property (nonatomic, copy) id<HumanProtocol> delegate; #import "tom.h" @interface Tom: NSObject <HumanProtocol> @end@implementation Tom - (void)name {} - (void)age {} @endCopy the code
Alternatively, write HumanProtocol as a separate class file and import it:
#import <Foundation/Foundation.h> #import "HumanProtocol.h" @interface Tom : NSObject <HumanProtocol> @property (nonatomic, copy) id<HumanProtocol> delegate; // call the protocol variable, or implement the protocol method @endCopy the code
2. Example
Is a base protocol that each new protocol needs to follow. @protocol is an annotation that defines a protocol, where @required indicates that the method must be implemented and @optional indicates that the method does not have to be implemented.
Application scenario: a person needs a Blog (Blog content can be different), the Blog must have common learning, sharing, etc. The requirements are as follows:
Need to create a person and Blog; 2. You need to create a Protocol to describe these functions. 3. Human-owned blogs should achieve these functions; Blogs need to follow the Protocol and implement it.
#import <Foundation/Foundation.h> @protocol BlogProtocol <NSObject> - (void)study; - (void)share; @end ---------------------------- #import <Foundation/Foundation.h> #import "BlogProtocol.h" @class Blog; @interface Person: NSObject @property (nonatomic,strong) Blog<BlogProtocol> * Blog; @end ---------------------------- #import <Foundation/Foundation.h> #import "BlogProtocol.h" @interface Blog : NSObject<BlogProtocol> @end #import "Blog.h" @implementation Blog - (void)study { NSLog(@"%s",__func__); } - (void)share { NSLog(@"%s",__func__); } @end ---------------------------- Person *p = [[Person alloc] init]; Blog *blog = [[Blog alloc] init]; P.log = Blog; If ([p.log respondsToSelector:@selector(study)]) {[p.log study]; } if ([p.blog respondsToSelector:@selector(share)]) { [p.blog share]; }Copy the code
3. Some points to note
The difference between protocol and inheritance: after inheritance, there is a default implementation, and the protocol as long as the declaration is not implemented; Classes of the same type can use inheritance, but classes of different types can only use protocols; Protocols can be used to store method declarations by extracting methods that are common to multiple classes and then making those classes comply with the protocol
The difference between a protocol and a Category is that a Category extends a class, and the class must have all its members. Different protocols can be implemented selectively. A category is implemented for a specific class, no other class, and the protocol allows any class to use and implement it; A Category is implemented by itself and not allowed to be overridden by other classes. The protocol only defines methods, and no classes are allowed to implement it themselves. Categories are limited by the nature of single inheritance, whereas protocols have no inheritance restrictions.
//// Protocol class @protocol SportProtocol <NSObject> @property (nonatomic,copy) NSString *sportType; - (void)playFootball; - (void)playBasketball; - (void)run; @end //// implementation class: Person #import <Foundation/ foundation.h > #import "sportprotocol.h" @interface Person: NSObject<SportProtocol> @end #import "Person.h" @implementation Person @synthesize sportType=_sportType; - (void)readSportType{ NSLog(@"%@",_sportType); } @end // Person *p =[[Person alloc]init]; // p.sportType = @"sport"; // [p readSportType];Copy the code
The above method uses @synthesize sportType=_sportType, and the sportType attribute is _sportType member variable synthesizes the accessor method.
//// class: Student #import "person.h" @interface Student: person@end // Student *stu = [[Student alloc]init]; // [stu playBasketball];Copy the code
A class can obey multiple protocols, and protocols can obey other protocols:
//// follows multiple protocol #import <Foundation/ foundation.h > #import "sportprotocol.h" #import "studyprotocol.h" @interface Person : NSObject<SportProtocol, StudyProtocol> @end //// protocol Complies with other protocols #import <Foundation/ foundation.h > #import "baseprotocol.h" @protocol StudyProtocol <BaseProtocol> - (void)studyEnglish; - (void)studyChinese; @endCopy the code
Protocol can declare both methods and properties; A class can adhere to multiple protocols; Protocol can also comply with other protocols; If the parent complies with the protocol of a class, the subclass automatically complies with the protocol.