This is my second article on getting started

Protocol Basic Concepts

  • Similar to the Java interface, an interface is a bunch of method declarations that have no implementation. In OC, an interface is implemented by a Protocol called a Protocol

Protocol syntax format

  • The definition of the Protocol
@protocol SportProtocol <NSObject>
- (void)playFootball;
- (void)playBasketball;
- (void)run;
@end
Copy the code
  • Class compliance agreement
    • A class can follow one or more protocols
    • Any class that follows Protocol owns all of Protocol’s method declarations
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Person : NSObject<SportProtocol>

@end
Copy the code

Protocol and inheritance

  • There is an implementation by default after inheritance, whereas protocol simply declares no implementation
  • Classes of the same type can use inheritance, but classes of different types can only use Protocol
  • Protocol 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

Protocol is different from category

  • A Category is a class that can extend methods with declarations and implementations, whereas Protocol has declarations but no implementations
  • Both categories and protocols can declare methods, not properties

Protocol Precautions

  • Protocol can declare properties in the @property form, except that properties declared in Protocol are only declared in the corresponding setter/getter methods and do not generate corresponding member variables

Protocol:

@protocol SportProtocol <NSObject>
@property (nonatomic,copy) NSString *sportType;
- (void)playFootball;
- (void)playBasketball;
- (void)run;
@end
Copy the code

Implementation class:

#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
Copy the code

**@synthesize sportType=_sportType**, which means that the sportType attribute synthesizes the accessor method with the _sportType member variable.

 Person *p =[[Person alloc]init];
 p.sportType = @"sport";
 [p readSportType];
Copy the code
  • If the parent complies with the Protocol of a class, the subclass automatically complies with the Protocol
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
@interface Person : NSObject<SportProtocol>
@end
Copy the code
#import "Person.h"
@interface Student : Person
@end
Copy the code
Student *stu = [[Student alloc]init];
[stu playBasketball];
Copy the code
  • A class can adhere to multiple protocols
#import <Foundation/Foundation.h>
#import "SportProtocol.h"
#import "StudyProtocol.h"
@interface Person : NSObject<SportProtocol,StudyProtocol>
@end
Copy the code
  • Protocols can also comply with other protocols. As long as a Protocol complies with other protocols, the Protocol automatically contains declarations of the other protocols
#import <Foundation/Foundation.h>
#import "BaseProtocol.h"
@protocol StudyProtocol <BaseProtocol>
- (void)studyEnglish;
- (void)studyChinese;
@end
Copy the code

The base agreement

  • NSObject is a base class that any other class inherits from
  • There’s also a Protocol called NSObject, which is a base Protocol
  • The most basic methods declared in the NSObject protocol (description, retain, release, and so on)
  • Every new protocol follows NSObject
#import <Foundation/Foundation.h>
@protocol BaseProtocol <NSObject>

@end
Copy the code

Protocol keywords

  • @required: This method must be implemented, the default is required (compile error if not implemented)
  • @optional: This method doesn’t have to be implemented
@protocol SportProtocol <NSObject>
@required
- (void)playFootball;
- (void)playBasketball;

@optional
- (void)run;
- (void)readSportType;
@end

Copy the code

Simple application

Requirements: A person needs an app, which must have the functions of learning, buying things, sharing and so on. Handling ideas: 1. A person and app need to be created; 2

#import <Foundation/Foundation.h>
@protocol DoSomethingProtocol <NSObject>
- (void)buySomething;
- (void)studyEnglish;
- (void)shareInfo;
@end
Copy the code
#import <Foundation/Foundation.h> #import "DoSomethingProtocol.h" @class APP; @interface Person: NSObject @Property (nonatomic,strong) APP<DoSomethingProtocol> * APP; @endCopy the code
#import <Foundation/Foundation.h>
#import "DoSomethingProtocol.h"
@interface APP : NSObject<DoSomethingProtocol>
@end

#import "APP.h"
@implementation APP
- (void)buySomething {
    NSLog(@"%s",__func__);
}
- (void)shareInfo {
    NSLog(@"%s",__func__);
}
- (void)studyEnglish {
    NSLog(@"%s",__func__);
}
@end
Copy the code
Person *p = [[Person alloc]init]; APP *app = [[APP alloc]init]; // If the app does not comply with the protocol, it will warn p.appp = app; If ([p.apprespondStoSelector :@selector(buySomething)]){[p.appbuysomething]; } if ([p.app respondsToSelector:@selector(studyEnglish)]) { [p.app studyEnglish]; } if ([p.app respondsToSelector:@selector(shareInfo)]) { [p.app shareInfo]; }Copy the code