Ios-runtime-A class has the same method as the protocol, according to the protocol name.

Source: Internet
Author: User

////VIEWCONTROLLER.M//Observertrampoline////Created by Rob Napier on 9/7/11.//Copyright (c) __mycompanyname__. All rights reserved.//#import "ViewController.h"#import "RNObserverManager.h"@protocolMyProtocol <NSObject>@optional- (void) dosomething;- (void) Dodo;@end@interfaceMyclass:nsobject <MyProtocol>@end@implementationMyClass- (void) dosomething {NSLog (@"dosomething:%@", self);}@end@interfaceMyclass1:nsobject@end@implementationMyClass1- (void) Dodo1 {NSLog (@"dodo1:%@", self);}- (void) Dodo {NSLog (@"Dodo to go.");}@end@implementationViewcontroller//@synthesize Observermanager=trampoline_;- (void) viewdidload {[Super viewdidload]; //This allows you to do something, such as a class that adheres to a protocol that can directly tune the protocol to execute the something written in that class.Nsset*observers = [Nsset setwithobjects:[myclassNew],[myclass1New], nil]; Self.observermanager=[[Rnobservermanager alloc] Initwithprotocol:@protocol(MyProtocol) observers:observers];  [Self.observermanager dosomething]; [Self.observermanager Dodo];}@end

Method of implementation

#import "RNObserverManager.h"@interfaceRnobservermanager () @property (nonatomic,ReadOnly, strong) Nsmutableset *observers, @property (Nonatomic,ReadOnly, strong) Protocol *protocol;@end@implementationRnobservermanager//Protocol Collection- (ID) Initwithprotocol: (Protocol *) Protocol observers: (Nsset*) Observers {if(self =[Super Init]) {_protocol=Protocol; _observers=[Nsmutableset setwithset:observers]; }    returnSelf ;}- (void) Addobserver: (ID) Observer {NSASSERT ([observer ConformsToProtocol:self.protocol],@"Observer must conform to protocol."); [Self.observers addobject:observer];}- (void) Removeobserver: (ID) Observer {[Self.observers removeobject:observer];}//NSObject--------//This function gives the overloaded party the opportunity to throw a signature of a function, followed by the forwardinvocation: to execute. -(Nsmethodsignature *) Methodsignatureforselector: (SEL) sel {nsmethodsignature*result=[Super Methodsignatureforselector:sel]; if(Result) {returnresult; }    //Look for a required method    structObjc_method_description desc =protocol_getmethoddescription (Self.protocol, SEL, yes, yes); if(Desc.name = =NULL) {        //couldn ' t find it. Maybe it ' s optionaldesc =protocol_getmethoddescription (Self.protocol, SEL, NO, YES); }      if(Desc.name = =NULL) {    //couldn ' t find it. Raise nsinvalidargumentexception[self Doesnotrecognizeselector:sel]; returnNil; }    return[Nsmethodsignature signatureWithObjCTypes:desc.types];}//true execution from Methodsignatureforselector: The returned nsmethodsignature. In this function, nsinvocation can be forwarded to multiple objects several times, which is also a flexible way. (Forwardingtargetforselector can only be turned to an object in the form of selector)- (void) Forwardinvocation: (Nsinvocation *) Invocation {SEL selector=[invocation selector];  for(IDResponderinchself.observers) {if([responder Respondstoselector:selector]) {[Invocation settarget:responder];        [Invocation invoke]; }    }}

Reference: http://www.cnblogs.com/biosli/p/NSObject_inherit_2.html

http://blog.csdn.net/yiyaaixuexi/article/details/8970734

Ios-runtime-A class has the same method as the protocol, according to the protocol name.

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.