Lan Yi Education Record

Source: Internet
Author: User

reviewed the contents of the agreement today.

Theory:

What is a protocol: a protocol is a rule, and a protocol is defined as a rule.

Classes in OC can comply with a protocol, and a class that adheres to a protocol is equivalent to having a capability.

@protocol protocol Name

@required declaration of properties and methods that must be followed

@optional declare properties and methods that are optional (CAN) adhere to

Default @required

@end

One class follows a protocol

[email Protected] Class name (category class name): Parent class name < protocol name >

B. Implementing the methods declared in the agreement

Inheritance of agreements

The inheritance of the agreement is equivalent to the merger of the agreement.

@protocol myprotocalb<myprotocala>

-(void) learn;

@end

A class can adhere to multiple protocols at the same time, separating the protocols with "," separators.

@interface Student:

Nsobject<myprotocolx,myprotocoly>

Protocol parameters

You can make a parameter an ID type but follow a protocol, which limits the parameters.

For example, a method requires an arbitrary type of object, but requires that the object must have a method. The parameter types are as follows:

-(void) method Name parameter description: (id< protocol name >obj;

For example:-(void) Dancewithobject: (id<dancedelegate>) obj;

Protocol return value type

+ (id<buttonprotocol>) Createbynum: (int) num

{

Switch (NUM)

{

Case 1:

return [[Loginbutton alloc]init];

Case 2:

return [[Regiterbutton alloc]init];

}

return nil;

}

Protocol Selector

-the reference to the protocol points to an agreement

Protocol *p = @protocol (nscopying);

-You can tell if a class has followed a protocol

BOOL B = [Student conformstoprotocol:p];

1, the protocol uses the situation: A class has an instance method, this instance method to the object passed over, restricts it must implement a specific method, and the passed object type is not limited.

2. Format

Protocol name: @protocol protocol Name Protocol name general: class name +delegate.

Protocol content: A specific method

Specific examples:

The instance method in the AA class has a protocol limit.

The BB class is a generic class.

Ii

In Aa.h :

Protocol wording

@protocol aadelegate < NSObject >

Agreement content

-(void) play;


@end


@interface Aa: NSObject

The object of the infection complies with the agreement

-(void) Playwithobject: (ID<AaDelegate>) anyobject;


@end


An example method of AA.M with protocol is written in the paper.


-(void) Playwithobject: ( ID <aa Delegate >) anyobject{

[anyobjectplay];

}

When introducing the AA Bb class in the Viewcontroller.

Create objects individually. AA class object, invoking an instance method to pass the BB class object into the Times warning: The BB class object must be compliant with the protocol of the instance method in the AA class.

Need to Bb.hin the Bb class:

The class in which the Protocol is introduced

#import "Aa.h"

Let the BB class comply with the agreement:

@interface Bb: NSObject <aaDelegate>

Implement the Protocol content in Bb.h.


Areas to be aware of:


Lan Yi Education Record

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.