OC Language-Protocol

Source: Internet
Author: User

1. Introduction of the agreement

1.1 What is an agreement

agreements are conventions and norms that require mutual compliance. such as prostitution agreement, we are responsible for the boss to write code, bug, release,boss is responsible for us pay, insurance, benefits , such as the HTTP protocol, two computers, abide by the same convention, can communicate with each other. as we communicate with each other, we abide by the Convention or norm of speaking Chinese.

2. OC in agreement

2.1OC Protocol Concept

The protocol in OC or iOS is a list of declarations for a set of methods (member functions); The declarative party does not need to implement these methods, The methods stipulated by the Agreement are implemented by the object of compliance. (Make laws and regulations and let others abide by them ).

steps for using the 2.2 OC protocol

making agreements-> Compliance Agreements-> Methods in implementing Protocols-> methods in the Invoke Protocol

2.3 OC Agreement formulation, Statement of agreement

Suppose the recruiting programmer has the ability to write code, fix bugs, and report work. ( the method to be implemented in the programmer class)

The protocol is defined as follows:

@protocol Coder <NSObject>    -(void) code;    -(void) debug;    -(void) report; @end

Explain:

@protocol, is the keyword used to customize the protocol ;

Coder, is the name of the agreement;

<nsobject>, in iOS, all classes, are directly or indirectly inherited from the NSObject class, all protocols are based on the NSObject protocol .

for NSObject, there is a class named NSObject in the system,            There is also an agreement named NSObject.

-(void) code;

-(void) debug;

-(void) report;

is the method of the Protocol declaration, does not need to implement, The method of implementing the Protocol is implemented by the object of complying with the agreement.

@end, indicating the end of the agreement declaration.

So far, I have made an agreement.

2.4 Compliance Agreement

working in the company, need to have the ability to set my agreement (can write code, adjust the bug, etc.), in other words, you need to abide by the agreement.

@interface student:nsobject <Coder>  -(void) eat; @end

Explain:

@interface Student:nsobject <coder>

The so-called compliance Agreement is to add <QFCoder> after the declaration of the class.

A class can follow multiple protocols, in angle brackets, separated by commas, Example: <Coder,Player> compliance with the Agreement requires the implementation of the methods stipulated in the agreement. At this point, we abide by the agreement to complete.

2.5 Implementation Protocol

the student class complies with the coder protocol, It is necessary to implement the Protocol method in the coder in @implementation.

@implementation student-(void) eat{    NSLog (@ "Custom method: This is your Yida"),}-(void) code{    NSLog (@ "method specified in the agreement: Code");}-(void ) debug{    NSLog (@ "The method prescribed by the Agreement: Debug")}-(void) report{    NSLog (@ "The method specified in the agreement: progress is normal");}  @end

Explain:

-(void) Eat

{

NSLog (@ "Custom method: This is your Yida");

}

This is the method of defining your own class, and we naturally want to implement

-(void) code

{

NSLog (@ "The method specified in the agreement:code");

}

This is the way we comply with the coder agreement, where we implement the coder protocol here. At this point, the protocol implementation is complete.

2.6 Calling protocol method

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

[DaNa eat];

[DaNa code];

[DaNa debug];

[DaNa report];

keywords in the 2.7 protocol

@required: Represents a method that must be implemented

@optional: means that it can be implemented or not, and you can choose a method

try to comment out the contents of the code method in @implementation, Xcode generates the method ' code ' in the Protocol not implemented warning, that is, the code method is not implemented.

if the Code,debug,report method is commented out, three warnings are generated. Therefore, if we are in the agreement statement, nothing is written by default is required, which is what must be achieved.

Change the agreement statement to the following form:

@protocol Coder <NSObject> @optional-(void) code;-(void) debug;-(void) report; @end

There is no warning, that is, Code,debug,report is an optional implementation.

Change the agreement statement to the following form:

@protocol Coder <NSObject> @optional-(void) code; @required-(void) debug;-(void) report; @end

Generates two warnings, that is, code is an optional implementation, and Debug,report must be implemented.

invocation of the 2.8 protocol method

because the protocol method may not all be implemented, If you rashly invoke an object that adheres to the Protocol method, And It happens that the object is not implemented, it will cause the program to crash; Therefore, we need to be careful about invoking protocol methods

We use the following form:

Determines whether the Dana object can respond to the report method. That is, there is no implementation of the report method,//If implemented, returns Yes, otherwise returns no if ([Dana Respondstoselector: @selector (report)]) {                [Dana];            Or can be written as follows, Performselector execution method            //[Dana Performselector: @selector (report)];        } if ([Dana Respondstoselector: @selector (familiarwithdatabase)]) {                [daNa performselector: @selector (familiarwithdatabase) ];        }

Judging from the running results of the program, we have achieved the report, Therefore, the method stipulated in the Protocol is printed: progress is normal. Familiarwithdatabase is not implemented, it is not called to.

Expand:

Nscoding protocol

We are learning to archive, to achieve encode and decode two methods, actually because we need to abide by the nscoding agreement. only objects of classes that adhere to the Nscoding protocol can be archived.

@protocol nscoding  -(void) Encodewithcoder: (Nscoder *) Acoder;  -(ID) Initwithcoder: (Nscoder *) Adecoder; @end

OC Language-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.