Protocol: Protocol (equivalent to the interface in C)
The protocol can be used at most in proxy mode and observer mode.Classes in OC only support single inheritance,
May comply with multiple protocols <Protocol 1, Protocol 2, Protocol 3...>Define a protocol named myprotocol @ protocol myprotocol @ end as long as a class complies with the Protocol, it can have all the methods in this Protocol declare a method that the class complies with a protocol: @ interface person: nsobject
<Myprotocol>@ End
: Inheritance
<> Compliance with the Agreement@ Protocol myprotocol
@ Required// The method declaration after this keyword requires implementation. If this keyword is not implemented, a warning is triggered (the default value is @ required)
@ Optional// The method declaration after this keyword does not require implementation (if not implemented, there will be no warning) @ end protocol can also comply with another protocol (just as the interface in C # can implement another interface). If one Protocol complies with another protocol, it can have all method declarations of the other protocol.
Base protocol:1. nsobject is a base class, the most fundamental and basic class. Any other class will eventually inherit it. 2. In fact, there is also a base protocol named nsobject, it is a base protocol, the most fundamental and basic Protocol 3. Many of the most basic methods stated in the nsobject protocol, for example, description, retain, and release. 4. We recommend that each new protocol comply with the nsobject protocol. You can use the Protocol to restrict object types: OBJ, the saved object must comply with the myprotocol protocol and inherit the person
Person <myprotocol> * OBJ = [[person alloc] init];When a class needs to follow the protocol, one way to reference the protocol is # import <myprotocol. h> and the other way is @ protocol myprotocol
(When this method is used, # import <myprotocol. h> in the corresponding. M file must be used .)
Summary:1. Protocol definition @ protocol name <nsobject> // method declaration list ..... @ End 2. How to comply with the Protocol-class Compliance Protocol @ Interface Class Name: parent class name <protocol name 1, protocol name 2 ......> @ end-Protocol Compliance Protocol @ protocol name <other protocol name 1, other protocol name 2 ......> @ end 3. keyword for method declaration in the protocol-@ required (default) requires implementation. If not, a warning will be issued-@ optional does not require implementation, how can this problem be solved? 4. when defining a variable, restrict the object stored in this variable to comply with a protocol class name <protocol name> * variable name; id <protocol name> variable name; nsobject <myprotocol> * OBJ; id <myprotocol> obj2; if the protocol is not followed, the compiler will warn 5 that the attribute declared in @ proerty can also be used as a compliance protocol restriction @ property (nonatomic, strong) class name <protocol name> * attribute name; @ property (nonatomic, strong) ID <protocol name> attribute name; @ property (nonatomic, strong) Dog <myprotocol> * dog; @ property (nonatomic, strong) ID <myprotocol> dog; 6. The protocol can be defined separately. in the H file, it can also be defined in a class. If this Protocol is only used in a class, the protocol should be defined in the class. If this protocol is used in many classes, it should be defined in a separate file. 7. categories can be defined in a separate file. H and. in the M file, it can also be defined in the original class. Generally, it is defined in a separate file and defined in the original class. Only the syntax is required.
Objective-C: 10_protocol