Protocol (Protocol) (20)

Source: Internet
Author: User

Protocol (Protocol)

Protocol is common in iOS, the protocol is a set of standards, similar to a list, it says that there are some things to be done, it depends on who signed the agreement, if who signed the agreement, then it will achieve (complete) the agreement required to complete the things.

Create Protocol,command+n, select Objective-c file, then write the protocol name in file, file type Select protocol. You can generate a. h file.

All protocols are declared in this. h file.

The declaration of the Agreement begins with @protocol, ends with @end, and puts in place some methods that need to be implemented, which are decorated with @required (which must be implemented) and @optional (which can be implemented without implementation).

@protocol Agreement name <nsobject>,nsobject in the <> number, it is no longer a class, but a protocol.

An example:

MyProtocol.h (Write protocol)

#import <Foundation/Foundation.h>

Claim Agreement

@protocol MyProtocol <nsobject>//in <> NSObject is not a class, but a protocol

@required//requirements are required. Must implement

-(void) eating;

-(void) drinking;

@optional//Optional. An optional implementation

-(void) shopping;

@end

Student.h (signed agreement)

#import <Foundation/Foundation.h>

#import "MyProtocol.h"//Introduction protocol

@interface student:nsobject<myprotocol>//Signing Agreement

@end

STUDENT.M (Implementation protocol content)

#import "Student.h"

@implementation Student

-(void) eating{

}

-(void) drinking{

}

-(void) shopping{

}

@end

MAIN.M Execution Protocol

#import <Foundation/Foundation.h>

#import "Student.h"

int main (int argc, const char * argv[]) {

@autoreleasepool {

Student *s1 = [[Student alloc]init];

[S1 eating];

[S1 drinking];

[S1 shopping];

}

return 0;

}

Protocol (Protocol) (20)

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.