Proxy extensions-implicit proxy and multicast proxy

Source: Internet
Author: User

Agent this thing, really do not want to talk about, is estimated to be an iOS developer can skillfully use, with notification and block, all use of memorized.

Here's a little talk about proxy extensions: implicit proxies and multicast proxies are actually very simple.

Implicit proxy: It is no longer necessary to abide by the protocol when defining the properties of the protocol, and the class implementing the method is not subject to the Protocol, because the Protocol method is defined in the NSObject classification.

Directly on the code bar, really is very simple:

1, the person class header file:

1 #import<Foundation/Foundation.h>2 3 /**4 A classification of nsobject, inside the Declaration (proxy) method5  */6 @interfaceNSObject (Myfenlei)7 8- (void) eat;9 Ten @end One  A /** - define a class -  */ the @interfacePerson:nsobject -  -@property (nonatomic,copy) NSString *name; -  + /** - Proxy Properties, no honor protocol, because the method of NSObject classification is to be implemented +  */ A@property (Nonatomic,weak)ID Delegate; at  -- (void) run; -  - @end

2. m files of person class:

1 #import "Person.h"2 3 @implementation Person4 5- (void) run{6NSLog (@"%@ is running .", self.name);7     8Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2* nsec_per_sec)), Dispatch_get_main_queue (), ^{9         if([Self.Delegaterespondstoselector: @selector (EAT)]) {Ten             //invoke the Eat method of the proxy object (declared in the classification of the NSObject) One[Self.Delegateeat]; A         } -     }); - } the  - @end

3. Call the Run method of the person object in the controller, and invoke the proxy method inside the Run method:

1 #import "ViewController.h"2 #import "Person.h"3 4 @interfaceViewcontroller ()5 6 @end7 8 @implementationviewcontroller{9Person *_per;Ten } One  A- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ -_per =[[Person alloc] init]; -_per.name =@"Xiao Ming"; the_per.Delegate= self;//Specify proxy -[_per Run];//call method, Invoke proxy method inside method - } -  + /** - Implementing protocol Methods +  */ A- (void) eat{ atNSLog (@"run, ready to eat."); - } -  - @end

It's over, it's gone. The implicit proxy is so simple. It is emphasized that the most important thing is that the protocol method is defined in the classification of NSObject. Understanding this point, is not very easy.

Below: Let's talk about multicast proxies.

Multicast proxies and notifications are much like an object that emits messages and listens to multiple objects. But better than the notification: 1, the key of the notification is easy to write wrong, 2, the logic of the notification is not clear.

Idea: In fact, is to define a proxy property, but this proxy property is an array, and then provide a way to add elements to this array. Then, when invoking the object method, loop through the property (array type). It's easy to understand this.

Said so much, said I am confused, rather than directly on the code to the happy. Anyway, the code is very small, it is not explained.

1. Person Class header file:

1 #import<Foundation/Foundation.h>2 3 /**4 defining protocols, declaring methods5  */6 @protocolPersondelegate <NSObject>7 8- (void) eatsomething;9 Ten @end One  A  - @interfacePerson:nsobject -  the /** - ways to add agents -  - @param delegate < #delegate description#> +  */ -- (void) AddDelegate: (ID)Delegate; +  A- (void) eat; at  - @end

2, Person class. m File:

1 #import "Person.h"2 3 @interfacePerson ()4 5 /**6 a proxy member variable, which is an array7  */8@property (nonatomic, strong) Nsmutablearray *delegates;9 Ten @end One  A @implementation Person -  - //adding elements to the proxy array the- (void) AddDelegate: (ID)Delegate{ -[Self.delegates AddObject:Delegate]; - } -  + //Key method: When invoking an object method, iterate over the array, this time the execution method -- (void) eat{ +      for(ID Delegate inchself.delegates) { A[(ID)Delegateeatsomething]; at     } - } -  - //lazy loading, initializing an array of proxy properties --(Nsmutablearray *) delegates{ -     if(_delegates = =Nil) { in_delegates =[Nsmutablearray array]; -     } to     return_delegates; + } -  the @end

3, OK, next we look at how to use the specific:

1Oneviewcontroller *ONEVC =[[Oneviewcontroller alloc] init];2Twoviewcontroller *TWOVC =[[Twoviewcontroller alloc] init];3     4Person *per = [personNew];5     6     //set up a multicast proxy7 [per ADDDELEGATE:ONEVC];8 [per ADDDELEGATE:TWOVC];9     Ten     //Invoke Proxy Method One[Per eat];

4, see, is called the method to the proxy attribute (array) to add elements, and then call the Eat method, the array is traversed, the method of execution. Over here

Oneviewcontroller and Twoviewcontroller all implement the protocol inside the-(void) eatsomething;
1 //Oneviewcontroller2 #import "OneViewController.h"3 4 @interfaceOneviewcontroller ()5 6 @end7 8 @implementationOneviewcontroller9 Ten- (void) eatsomething{ One      ANSLog (@" One"); - } -  the @end -  - //Twoviewcontroller. - #import "TwoViewController.h" +  - @interfaceTwoviewcontroller () +  A @end at  - @implementationTwoviewcontroller -  -- (void) eatsomething{ -      -NSLog (@" Both"); in } -  to @end

Well, that's it, isn't it easy?

Proxy extensions-implicit proxy and multicast 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.