Use of iOS Information interaction Protocol

Source: Internet
Author: User

If certain methods are defined in a protocol, and a class implements the protocol, the class must implement these methods. In other words, the protocol is a common set of method declarations, who implement the Protocol, who is responsible for implementing these methods, or there will be a yellow warning. The protocol can extend an existing protocol. The keyword for the agreement is protocol, starting with @protocol and ending with @end. When implementing a protocol in a class, you only need to add a < protocol name after the class name; Here's a look at the code:

Define a protocol first: Eat.h

[Plain]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. @protocol eat <nsobject>//protocol eat extended the protocol NSObject.h
    3. -(void) eat;
    4. @end
The above extended NSObject protocol is not implemented, because the class inheriting from NSObject has inherited the implementation of the NSObject protocol

Create a class below: Human.h

[Plain]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. #import "Eat.h"
    3. @interface Human:nsobject <eat>
    4. @end
Human.m

[Plain]View Plaincopy
    1. #import "Human.h"
    2. @implementation Human
    3. -(void) Eat
    4. {
    5. NSLog (@ "eat defined in the agreement");
    6. }
    7. @end
Called in MAIN.M:

[Plain]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. #import "Eat.h"
    3. #import "Human.h"
    4. int main (int argc, const char * argv[])
    5. {
    6. @autoreleasepool {
    7. Human *human =[[human alloc] init];
    8. [Human eat];
    9. }
    10. return 0;
    11. }


2012-03-18 14:35:29.099 category1[1752:403] defined in the agreement. Eat



Very simple, in addition, in the new version of the Objective-c, added some options for the protocol, @optional, @required, the protocol must be implemented in the method, otherwise it will be error, but if the @optional modified, there is no such restriction, The method that must be implemented by default is equivalent to a @required modification, such as the above code, and we can make the following changes:

Eat.h

[Plain]View Plaincopy
    1. #import <Foundation/Foundation.h>
    2. @protocol eat <nsobject>//protocol eat extended the protocol NSObject.h
    3. -(void) eat;
    4. @optional
    5. -(void) Playguitar;
    6. @required
    7. -(void) sleep;
    8. @end

In this case, the sleep method and the Eat method must also be implemented, and the Playguitar method is optional.

Use of iOS Information interaction Protocol

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.