An overview of the

A Protocol is similar to an interface of a class, except that it has no parent class and cannot define instance variables.

Only declared, not implemented. A protocol can only define a common set of interfaces, but cannot provide concrete implementation methods. That is, it tells you what to do, but it doesn’t care how. The implementation is implemented in classes that comply with this protocol.

The basic function

  • Declare several methods (cannot declare member variables)
  • As long as a class complies with the protocol, it owns all of the method declarations in that protocol, and class objects can call methods directly
  • As long as the parent class adheres to a protocol, so does its subclass
  • Similar to Java’s “interface,” but with more functionality
  • A class can follow more than one protocol
  • Agreements can be kept. A protocol that complies with another protocol can have method declarations in another protocol

The protocol can declare properties in the @property form, but only the corresponding setter/getter method declarations, and no corresponding member variables are generated

The base agreement


is the base protocol, the most basic protocol, and it declares many of the most basic methods, such as description, retain, and release. It is recommended that every new protocol comply with the NSObject protocol

NSObject is a base class, the most basic class that any other class will eventually inherit from.

use

1. Defining protocols can be defined in a separate. H file or in a class

@protocol PDCustomProtocol <NSObject> @protocol PDCustomProtocol <NSObject> @Required (default) - (void)playBasketball; @optional - (void)run; @endCopy the code
  • Modify the keyword of a method

@required: Classes that require compliance must be implemented, otherwise a warning is issued, but no error is reported. Optional: No implementation required and no warning if not implemented.

If the protocol is only used in a class, you should define the protocol in that class

If the protocol is used in many classes, it should be defined in a separate.h file

New Protocol file:

It will generate a.h file

#import “protocol filenames. H” or @protocol name. Using the latter tells the following code that this is a protocol, but does not know what is in the protocol. #import is usually used only when the content of the protocol is used. The @protocol protocol name is commonly used in actual development.

  • Class compliance agreement
@interface Class name: parent class name < protocol name 1, protocol name 2> @endCopy the code

Such as:

@interface Person : NSObject <PDCustomProtocol>

@end
Copy the code
  • Agreement Compliance agreement
@protocol Protocol name < Other protocol name 1, Other protocol name 2> @endCopy the code
  • Restrict an object to a protocol

When you define a variable, you can restrict the object that the variable holds to a certain protocol. The compiler warns if the protocol is not followed. Class name < protocol name > * Variable name;

NSObject<MyProtocol> *aObject; Or @property (nonatomic, strong) Dog<MyProtocol> * Dog;Copy the code

application

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

Difference between Protocol and inheritance

Inheritance is inherited along with the implementation of the method, whereas protocol only has declarations that are not implemented; Classes of the same type can use inheritance, but classes of different types can only use Protocol

Difference between Protocol and Category

A Category can be given to a class extension method, both declared and implemented; Protocol is only a declaration, not an implementation. Same as: Category and Protocol can declare methods, not properties.