Protocol (Protocol) is a very important language feature in objective-c, conceptually, very similar to the interface in Java. A protocol is actually a collection of associated methods (for the convenience of the following, we name the protocol MyProtocol). The method in the protocol is not implemented by the Protocol itself, but rather by other classes that follow this protocol. In other words, the protocol MyProtocol only completes the declaration of the Protocol function, regardless of the specific implementation of these protocol functions.
The syntax for declaring a protocol is very simple:
[CPP]View Plaincopy
- @protocol MyProtocol <NSObject>
- @required
- -(void) Protocolnamea: (nsstring*) string;
- @optional
- -(void) Protocolnameb: (nsstring*) string;
- @end
The first line is to declare the name of the agreement MyProtocol. The nsobject in the angle brackets itself is also a protocol that defines a number of basic protocol functions, such as Performselector,iskindofclass,respondstoselector,conformstoprotocol, Retain,release and so on.
The Protocol interface is divided into two categories: required and optional. Required as the name implies is that the class that adheres to this Protocol "must" implement the interface, while the optional can be implemented or not implemented. The definition of the Protocol interface is the same as the normal function definition.
The last line @end represents the end of the protocol definition. The definition of this protocol is usually in the. h file.
Define a class to follow this protocol:
[CPP]View Plaincopy
- @interface MyClass <myProtocol>
- @interface myclass:nsobject<myprotocol>
- @interface Myclass:nsobject<myprotocol, nscoding>
The above are three different cases respectively. When compiling, the compiler automatically checks to see if MyClass implements the necessary (@required) interfaces in the MyProtocol. If it is not implemented, a warning message is issued. It is also important to note that if you have subclasses that inherit from MyClass, these subclasses will automatically follow the protocol that MyClass follows and can also overload these interfaces.
Why do I need an agreement?
Apple's official documentation points to three reasons:
To declare methods that others is expected to implement
To declare the interface to a object while concealing its class
To capture similarities among classes that is not hierarchically related
In fact, there is a fourth important reason, that is, to reduce the complexity of inheriting classes. A classic example is the Uitableviewcontroller class within the iOS UI framework. Without the "protocol" function, the user would have to choose to use inherited and overloaded interfaces to implement complex UI controls and other event handling-a much greater challenge to the design of the base class. For a table view like this, a good way to do this is to use the protocol, the interface in the protocol to control different data sources and a variety of complex user operations. In Uikit, two good protocol uitableviewdelegate,uitableviewdatasource are designed to achieve uitableviewcontroller control. Any class that follows these two protocols can achieve control of the UITableView.
About the use of ID types: (do not like a friend, can skip this part)
The ID type is a common type in iOS and is somewhat similar to the void* type of C language. The compiler cannot check the actual type of a variable that is defined as an ID type, and the identification of the ID type occurs at the runtime stage. But we can use id<protocol_name> obj; this syntax form at compile time allows the compiler to know that obj can only send messages in Protocol_name if the message being sent is not in Protocol_name. The compiler will give a warning message "Instance method ' xxxx: ' not found ...". This situation is more used for proxy mode implementations, such as a class that has a delegate property:
[CPP]View Plaincopy
- ID <myProtocol> delegate;
Thus, during the compile phase we can know that the message sent with delegate is not the message in the MyProtocol it follows. Well, here I drilled the Niu Jiao Jian, I put the id behind the <myProtocol> deleted, and then use delegate send a message that does not exist in MyProtocol, the result of the compiler or "Instance method ' xxxx: ' Not found ... "warning message. Even more strangely, when sending a message that exists in MyProtocol, the compiler does not have such a warning message. These two tests do not indicate that the previous explanation was wrong, but that id<myprotocol> delegate was written to make it easier to know that the delegate followed the MyProtocol protocol.
OBJECTIVE-C Protocol (Protocol)