OBJECTIVE-C (Protocol Protocol)

Source: Internet
Author: User

Protocol protocol

Protocol: Used to declare a method

1. Definition of the Agreement

@protocol Agreement name <NSObject>

Method declaration List ....

@end

2. How to comply with the agreement

1> Class Compliance Agreement

@interface Class Name: Parent class name < protocol name 1, protocol name 2>

@end

2> Protocol Compliance Agreement

@protocol Agreement name < Other protocol name 1, other protocol name 2>

@end

3. Keywords for method declarations in the agreement

1> @required (default)

Requires implementation, and if not implemented, a warning is issued

2> @optional

Does not require implementation, how can there be no warning

4. When defining a variable, limit the object that this variable holds to a protocol

Class name < protocol name > * variable name;

id< protocol name > variable name;

Nsobject<myprotocol> *obj;

Id<myprotocol> Obj2;

If the corresponding protocol is not followed, the compiler warns

The attributes declared in [email protected] can also be used as a limit to comply with the agreement

@property (Nonatomic, Strong) class name < protocol name > * attribute name;

@property (nonatomic, strong) id< protocol name > attribute name;

@property (nonatomic, strong) dog<myprotocol> *dog;

@property (nonatomic, strong) id<myprotocol> dog2;

6. The agreement can be defined in a separate. h file and can also be defined in a class

1> If this protocol is used only in a class, the protocol should be defined in that class

2> If this protocol is used in many classes, it should be defined in a separate file

7. Classifications can be defined in separate. h and. m files and can also be defined in the original class

1> in general, are defined in a separate file

2> defines the classification in the original class, only requires the ability to read the grammar

#import<Foundation/Foundation.h>@protocolMyProtocol <NSObject>//compliance with other agreements, the other agreement names are written in brackets < >//The default is to implement- (void) test;//declaring Methods//required need to be implemented, no implementation will only issue a warning//Optional does not need to implement@required- (void) test1;- (void) test2; @optional- (void) test3;@end/** * * * above is MyProtocol.h file *******/ #import<Foundation/Foundation.h>
@protocol MyProtocol;//can have multiple protocol <mtprotocol at the same time, myprotocol2>@interfacePerson:nsobject <MyProtocol> @property (nonatomic, strong) id<myprotocol> obj;@end/** * * * above is Person.h file *******/ #import<Foundation/Foundation.h>#import "MyProtocol.h" @inplementation person- (void) test{}- (void) test1{}- (void) test2{}@end
/******The above are person.m files***********/

#import <Foundation/Foundation.h>
#import "MtProtocol.h"
#import "Person.h"
int main ()
{

Person *p = [[Person alloc] init];
  

return 0;
}

Base protocol:<nsobject> default adherence to the base protocol

OBJECTIVE-C (Protocol Protocol)

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.