iOS Review note 11: Protocols and proxies

Source: Internet
Author: User

a function
You can declare a method in a protocol (you cannot declare a variable).
As long as a class adheres to this protocol, it is equivalent to having all the method declarations in the agreement.
The parent class is a quasi-gatekeeper protocol, and subclasses also have methods in the protocol.
The agreement may also be subject to another agreement.
Base protocol NSObject (also the base class), the NSObject base class is also quasi-obey the base protocol.
The base protocol contains common memory management methods, such as the Release,retain method.


Two definitions
1 Protocol Definitions
@protocal Agreement name <NSObject>
Declaring methods
@end


2 Compliance Agreement
Class 2.1
@interface Class Name: Parent class name < protocol name 1, protocol name 2...>
@end


2.2 Protocol
@protocal protocol Name < protocol name 1, protocol name 2...>
@end


3 Key Words
@required to please implement, do not implement a warning
@optional Optional Implementation


4 Define a variable that must be subject to a protocol
NSObject < agreement name > obj1;
ID < agreement name > obj2;
@property (nonatomic, strong) ID < protocol name > P;


Three code examples

Myprotocal.h@protocal myprotocal <nsobject>-(void) protocoldefaultfun (), @required//default, requires implementation, does not implement report warning-(void ) Protocolrequiredfun (); @optional//optional implementation-(void) protocoloptionalfun (); @end

Mulprotocal.h@protocal mulprotocal <nsobject>-(void) mulprotocoldefaultfun (); @end

Person.h#import <Foundation/Foundation.h> @protocal myprotocal; @protocal mulprotocal;//or @protocal Myprotocal, mulprotocal;//as long as one class complies with an agreement, it has all the methods declared in the agreement @interface Person:object <myprotocal, mulprotocal> @end


Person.m#import "Person.h" #import "MyProtocal.h" #import "MulProtocal.h" @implementation person <myprotocal, mulprotocal>-(void) Protocoldefaultfun () {}-(void) Protocolrequiredfun () {}-(void) Protocoloptionalfun () {}-(void ) Mulprotocoldefaultfun () {} @end


Main.m#import <Foundation/Foundation.h> #import "MyProtocal.h" #import "Person.h" int main () {// Declares an object that complies with myprotocal nsobject<myprotocal>* obj1 = [[Person alloc] Init];obj1 = nil;id<myprotocal> Obj2 = [[ Person Alloc] Init];obj2 = nil;//return 0;}


Four agents

Ticketdelegate.h@protocol ticketdelegate <nsobject>-(int) leftticketsnumber; @end

Agent.h@protocol ticketdelegate, @interface agent:nsobject <ticketDelegate> @end//Agent.m#import "Agent.h" # Import "TicketDelegate.h" @implementation agent-(int) Leftticketsnumber{return 1;} @end

Person.h@class agent, @class ticketdelegate, @interface person:nsobject-(void) buyticket;//Proxy Properties @property (nonatomic , retain) ID <ticketDelegate> delegate; @end

Person.m#import "Person.h" #import "Agent.h" @implementation person-(void) buyticket{if (_delegate & [_delegate Leftticketsnumber] > 0) {NSLog (@ "buy ticket");}} @end

Main.m#import "Person.h" #import "Agent" int main () {@autopool {person* p = [[Person alloc]init]; agent* a = [[Agent alloc] init];p. Delegate = A; [P buyticket];} return 0;}




iOS Review note 11: Protocols and proxies

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.