How to Create a proxy in Objective-C and create an objective-c

Source: Internet
Author: User

How to Create a proxy in Objective-C and create an objective-c

First, we need to create a class. In order to facilitate understanding, we will simulate finding an intermediary to rent a house in real life. Therefore, we need to create a Person class first.

1. Define the proxy protocol in Person. h and complete the proxy method declaration: the code is as follows:

1 // Person. h文2 2 3 # import <Foundation/Foundation. h> 4 5 @ class Person; 6 7 // 1 Define the proxy protocol 8 @ protocol PersonDelegate <NSObject> 9 10 // optional method 11 @ optional12-(void) personFindHouse :( Person *) person; 13 // required method 14 @ required15 16 @ end17 18 @ interface Person: NSObject19 20 @ end

Note: The optional methods in the proxy Protocol can be implemented or not, but the necessary methods must be implemented.

2. Define proxy properties: the code is as follows:

1 // Person. h文2 2 3 # import <Foundation/Foundation. h> 4 @ class Person; 5 // 1 Define the proxy protocol 6 @ protocol PersonDelegate <NSObject> 7 // optional method 8 @ optional 9-(void) personFindHouse :( Person *) person; 10 // required method 11 @ required12 13 @ end14 15 @ interface Person: NSObject16 @ property (nonatomic, copy) NSString * name; 17 // 2 define proxy attributes 18 @ property (nonatomic, weak) id <PersonDelegate> delegate; 19-(void) zuFang; 20 @ end

3. Call the proxy method (notification) to send a message to the Proxy: the code is as follows:

1 // Person. m file 2 # import "Person. h "3 4 @ implementation Person 5-(void) zuFang 6 {7 NSLog (@" % @ -- rent a house ", self. name); 8 9 // 3 call the proxy method (notification) to send a message to the proxy 10 if ([self. delegate respondsToSelector: @ selector (personFindHouse :)]) 11 {12 [self. delegate personFindHouse: self]; 13 14} 15} 16 @ end

The first line of the code above is to determine whether the object has implemented the personFindHouse: This method

Now we need to define a ZhongJie class to use this proxy.

1. Make this class comply with the proxy Protocol: the code is as follows:

1 // ZhongJie. h file 2 # import <Foundation/Foundation. h> 3 # import "Person. h" 4 5 @ interface ZhongJie: NSObject <PersonDelegate> 6 7 @ end

2. Implement proxy method: the code is as follows:

1 // ZhongJie. m file 2 # import "ZhongJie. h "3 4 @ implementation ZhongJie 5 6-(void) personFindHouse :( Person *) person 7 {8 NSLog (@" found house "); 9} 10 11 @ end

3. Set proxy properties: the code is as follows:

1 // ViewController. m file 2 # import "ViewController. h "3 # import" Person. h "4 # import" ZhongJie. h "5 @ interface ViewController () 6 7 @ end 8 9 @ implementation ViewController10 11-(void) viewDidLoad {12 [super viewDidLoad]; 13 // Do any additional setup after loading the view, typically from a nib.14 ZhongJie * zj = [[ZhongJie alloc] init]; 15 16 Person * p = [[Person alloc] init]; 17 18 p. name = @ "someOne"; 19 // 3 set proxy properties 20 p. delegate = zj; 21 22 [p zuFang]; 23 24} 25 26 27 @ end

 

In this way, a proxy is created and an object uses this proxy.

 

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.