Dark Horse programmer-protocol (protocol), dark horse-protocol

Source: Internet
Author: User

Dark Horse programmer-protocol (protocol), dark horse-protocol
------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! -------

  • Can be used to declare a lot of methods (member variables cannot be declared)
  • As long as a class complies with this Protocol, it is equivalent to having all the method declarations in this Protocol.
  • As long as the parent class complies with a certain protocol, it is equivalent to the sub-class also complies
// Define a protocol named MyProtocol. h @ protocol MyProtocol <NSObject> @ required // requires implementation. If you do not know what to do, a warning will be issued. If you do not write anything, the default value is @ required-(void) test;-(void) test2; @ optional // not required-(void) test3; @ end Person. h @ protocol MyProtocol; // pre-statement of the protocol, similar to @ class @ interface Person: NSObject <MyProtocol> @ end Person. m # import MyProtocol. h; @ implementation Person-(void) test {}- (void) test2 {}@ end
  • One protocol can comply with multiple other protocols. Multiple protocols are separated by commas (,).
  • An agreement that complies with other protocols is equivalent to having a method statement in other Protocols

@ Protocol name <protocol 1, protocol 2>

@ End

1. Class Compliance Agreement: @ interface Class Name: parent class name <protocol name 1, protocol name 2> @ end2 protocol Compliance Agreement: @ protocol name <other protocol name 1, other protocol name 2> @ end1.
  • NSObject is a base class, the most fundamental and basic class. Any other class will eventually inherit it.
  • In fact, there is also a protocol named NSObject, which is a base protocol and the most fundamental and basic protocol.
  • The NSObject Protocol declares many basic methods, such as description, retain, and release.
  • We recommend that each new protocol comply with the NSObject protocol.
The object stored in obj3 must comply with the MyProtocol protocol; NSObject <MyProtocol> * obj3; id <MyProtocol> obj3; obj4 is required, and the saved object must comply with MyProtocol3, and inherits the attributes declared in the PersonPerson <MyProtocol3> * obj4; @ property can also be used as a protocol-compliant constraint @ property (nonatomic, strong) class Name <protocol name> * attribute name; @ property (nonatomic, strong) id <protocol name> attribute name; Protocol can be defined separately. in the H file, it can also be defined in a class. 1. If this Protocol is only used in a class, the protocol should be defined in this class. 2. If this protocol is used in many classes, the classification should be defined in a separate file and can be defined separately. h and. the m file can also be defined in the original Class 1. Generally, it is defined in a separate file 2 and defined in the original class category, only the syntax # import "TicketDelegate. h "@ interface Person: NSObject-(void) buyTicket; // has a proxy attribute. The proxy class name must comply with the TicketDeltegate Protocol @ property (nonatomic, retain) id <TicketDelegate> delegate; @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.