A simple example of OC protocol and Proxy

Source: Internet
Author: User

Create a ticket agreement. The agreement stipulates whether the output will be left

1 #import <Foundation/Foundation.h>23@protocol buyticketsdelegate < Nsobject>45@required6 -(void) check; 7 8 @end
View Code

Create a proxy Agent compliance protocol

1 #import "Buytickets.h"2 @interfaceAgents:nsobject<buyticketsdelegate>3 4 @end5 6 7 @implementationAgents8 9- (void) check{TenNSLog (@"There are still tickets left ."); One } A  - @end
View Code

Create a person class that contains a proxy object that adheres to the protocol

1 @interfacePerson:nsobject2 3@property (nonatomic, assign)ID<BuyticketsDelegate>Delegate;4 5- (void) Buy;6 7 @end8 9 Ten #import "Person.h" One @implementation Person A  -- (void) buy{ - [_delegate check]; the } -  - @end
View Code

In the main function, specify a proxy

1 intMainintargcConst Char*argv[]) {2 @autoreleasepool {3        4Person *person =[[Person alloc]init];5Agents *age =[[Agents alloc]init];6Person.Delegate=Age ;7 [person buy];8 9         Ten     } One     return 0; A}
View Code

A simple example of OC protocol and Proxy

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.