Message forwarding in iOS

Source: Internet
Author: User

Message forwarding in iOS

 

Well, running is a good thing. In Objective-C, this feature can help us do a lot of things.

First, this feature changes code decisions from compilation and linking to running, so that we can use this feature to do something that can only be done during running, including:

1. swizzling (implement the exchange of two methods)

2. dynamic methods (you can add methods to a class during running. This blog will mainly explain this)

3. References (you can associate a class with an object using a key)

4. introspection (determine whether an object can call a method)

5. Use the emoji character as the method name

 

Message Forwarding is divided into two parts. One is to add an implementation of the method that cannot respond to this class, and the other is to let another object respond to the method called by the user.

 

First, we know that when we call a method of an object, we will use * isa to find the implementation IMP of this method in this class. If this class cannot be found, it will look for the parent class. When NSObject cannot be called, the program will crash out. However, before the crash operation, we provided a mechanism to dynamically Add the implementation of this method or let another object respond to this method.

 

Let's first look at how to add a method dynamically. For example, we call the doFoo method that the object cannot respond to, but we have a method implementation declaration in the class:

 

void fooMethod(id obj, SEL _cmd) {        NSLog(@hehe);}

We want to use this method to implement the doFoo method, so we need to rewrite the + (BOOL) resolveInstanceMethod :( SEL) aSEL method, as follows:

 

 

+(BOOL)resolveInstanceMethod:(SEL)aSEL{    if(aSEL == @selector(doFoo)){        class_addMethod([self class],aSEL,(IMP)fooMethod,v@:);        return YES;    }    return [super resolveInstanceMethod:aSEL];}

 

After hitting the breakpoint, we can see that the console outputs hehe.

The above is the process of dynamically adding the implementation of this method.

 

The second is to let another object respond to this method.

 

The system first enters the-(id) forwardingTargetForSelector :( SEL) aSelector Method for forwarding. We need to rewrite this method as follows:

 

- (id)forwardingTargetForSelector:(SEL)aSelector{    if(aSelector == @selector(doFoo)){        return self.alternativeObject;    }    return [super forwardingTargetForSelector:aSelector];}

 

After obtaining this object, the system uses the-(void) forwardInvocation :( NSInvocation *) invocation method to uniformly execute the method, as shown below:

 

-(void)forwardInvocation:(NSInvocation *)invocation{    SEL invSEL = invocation.selector;        if([self.alternativeObject respondsToSelector:invSEL]) {        [invocation invokeWithTarget:self.alternativeObject];    } else {        [self doesNotRecognizeSelector:invSEL];    }}

All right, we break the response method in alternativeObject, and we find that it has been executed !~

 

 

 



 

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.