Implementation of message forwarding in iOS

Source: Internet
Author: User


Well, at run time, the runtime is a good thing. In the Objective-c language, this feature can help us do a lot of things.

The first feature is to make the code decisions from compile and link to run, so that we can use this feature to do something that can only be done at runtime, including:

1.swizzling (Exchange two implementations of methods)

2. Dynamic methods (You can add methods to a class at run time, this blog will mainly explain this)

3. Related references (you can associate a class and an object with a key)

4. Introspection (Determines whether an object can call a method)

5. Use the emoji character as the method name


We are divided into two parts to explain message forwarding, one is to add an implementation of an unresponsive method in this class, and one is to let another object respond to a user invocation.


The first thing we know is that when we call a method of an object, the *isa really looks for the implementation of the method in this class imp, if this class is not found, it will look for the parent class, when the walk to NSObject can not be called, the program will crash out. But before crash, the runtime provides us with a mechanism that can dynamically increase the implementation of this method or let another object respond to this method.


Let's take a look at the dynamic add a method, 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, then we need to rewrite+(BOOL) Resolveinstancemethod: (SEL) Asel method, as follows:

+ (BOOL) Resolveinstancemethod: (SEL) asel{    if (Asel = = @selector (Dofoo)) {        Class_addmethod ([Self Class],asel, ( IMP) Foomethod, "[email protected]:");        return YES;    }    return [Super Resolveinstancemethod:asel];}

By hitting the breakpoint, we can see the console output "hehe".

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


The second one is to have another object respond to this method.


The system will walk in first -(ID) forwardingtargetforselector: ( SEL ) Aselector method to forward, we need to rewrite this method, as follows:

-(ID) Forwardingtargetforselector: (SEL) aselector{    if (aselector = = @selector (Dofoo)) {        return Self.alternativeobject;    }    return [Super Forwardingtargetforselector:aselector];}

once the system has obtained this object, it will use the -(void) forwardinvocation: ( nsinvocation *) Invocation method to unify the implementation of the method, specifically as follows:

-(void) Forwardinvocation: (nsinvocation *) invocation{    SEL invsel = invocation.selector;        if ([Self.alternativeobject Respondstoselector:invsel]) {        [invocation invokeWithTarget:self.alternativeObject] ;    } else {        [self Doesnotrecognizeselector:invsel];}    }

Okay, we're in the Alternativeobject response method break point, we found out! ~


The above is to explain the ~ ~




Implementation of message forwarding in iOS

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.