iOS underlying development message mechanism (iii) dynamic method resolution

Source: Internet
Author: User

Preface

What happens if we send a message to an object in Objective C that it cannot handle? According to the previous article, "The message of cocoa," we know that the message is sent through Objc_send (ID, SEL, ...) to achieve, it will first in the object's class object's Cache,method list and the parent class object's cache, method list Find the SEL corresponding IMP in turn, if not found and implement a dynamic method resolution mechanism will be the resolution, if not implemented dynamic method resolution mechanism or resolution failure and implement the message forwarding mechanism will enter the message forwarding process, otherwise the program crash. In other words, if the dynamic method resolution and message forwarding are provided at the same time, then the dynamic method resolution is preceded by the message forwarding, and only if the dynamic method resolution is still unable to correctly selector the implementation of the resolution, the message forwarding will be attempted. In the previous article, I did not explain the dynamic method resolution in detail, so this article will be described in detail.

1 #import <Foundation/Foundation.h>23@interface  prettygirl:nsobject  45 -(void) loveme; 6 7 @end
1 #import "PrettyGirl.h"2 3 @implementationPrettygirl4 5-(void) Loveme6 {7NSLog (@"pretty Girl Love me!");8 }9 Ten @end
1 #import<Foundation/Foundation.h>2 #import "PrettyGirl.h"3 4 intMain (intargcConst Char*argv[])5 {6 7 @autoreleasepool {8         9Prettygirl *xiaoqian=[[Prettygirl alloc]init];Ten [Xiaoqian Loveme]; One [Xiaoqian Movie]; A [Xiaoqian release]; -          -     } the     return 0; -}

Run results

1  the-Ten- -  +:Geneva:25.899deeointomethod[4010:303] Pretty Girl Love me!2  the-Ten- -  +:Geneva:25.901deeointomethod[4010:303]-[prettygirl Movie]: Unrecognized selector sent to instance0x1002009803  the-Ten- -  +:Geneva:25.902deeointomethod[4010:303] * * * * terminating app due to uncaught exception'nsinvalidargumentexception', Reason:'-[prettygirl Movie]: Unrecognized selector sent to instance 0x100200980'4FirstThrowCall Stack:5 (6     0Corefoundation0x00007fff8a13925c__exceptionpreprocess +1727     1LIBOBJC. A.dylib0x00007fff8c866e75Objc_exception_throw + +8     2Corefoundation0x00007fff8a13c12d-[nsobject (NSObject) Doesnotrecognizeselector:] +2059     3Corefoundation0x00007fff8a097272___forwarding___ +1010Ten     4Corefoundation0x00007fff8a096df8_cf_forwarding_prep_0 + - One     5Deeointomethod0X0000000100001ACDMain + the A     6Deeointomethod0x0000000100001a44Start + the -     7???0x0000000000000001 0x0+1 - ) theLibc++abi.dylib:terminating with uncaught exception of type NSException

[Prettygirl Movie]: Unrecognized selector sent to instance 0x100200980 this sentence means that the method is not found in the method list, so the program crash

Well, here we use dynamic resolution to solve this problem.
1#import <Foundation/Foundation.h>2#import"PrettyGirl.h"3 4 intMain (intargcConst Char*argv[])5 {6 7 @autoreleasepool {8         9Prettygirl *xiaoqian=[[Prettygirl alloc]init];Ten [Xiaoqian Loveme]; One [Xiaoqian Movie]; A [Xiaoqian release]; -     } -     return 0; the}
1 #import <Foundation/Foundation.h>23@interface prettygirl:nsobject 4 5 -(void) loveme; 6 7 @end
1#import"PrettyGirl.h"2#include <objc/runtime.h>3 4 voidPutoncoat (NSString *str)5 {6NSLog (@">> putoncoat.");7 }8 9 Ten @implementation Prettygirl One  A-(void) Loveme - { -NSLog (@"pretty Girl Love me!"); the } -  -+(BOOL) Resolveclassmethod: (SEL) SEL - { +    if(sel==@selector (Movie)) -    { +Class_addmethod ([Selfclass],sel, (IMP) Putoncoat,"[Email protected]:@"); A    } at     return[Super Resolveclassmethod:sel]; - } -  -+(BOOL) Resolveinstancemethod: (SEL) SEL - { -     if(sel==@selector (Movie)) in     { -Class_addmethod ([Selfclass],sel, (IMP) Putoncoat,"[Email protected]:@"); to     } +     return[Super Resolveclassmethod:sel]; - } the  *@end
The first parameter of the Class_addmethod method is the class to execute [self class], the second parameter is the method name SEL, the third parameter is the method pointer to replace, and the last method is the parameter
Parameter explanation:
I: return int type, v means no return value
@: Parameter ID (self)
: SEL (_cmd)
@:id (str)

iOS underlying development message mechanism (iii) dynamic method resolution

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.