cool hit counter How does runtime find the corresponding IMP address via selector?_Intefrankly

How does runtime find the corresponding IMP address via selector?


The class object has a list of class methods and instance methods, and the list records the method's noun, arguments, and implementation, while the selector is essentially the method name, and runtime can find the method's corresponding implementation in the list by this method name.

struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        
    const char *name                                         
    long version                                             
    long info                                                
    long instance_size                                       
    struct objc_ivar_list *ivars                             
    struct objc_method_list **methodLists                    
    struct objc_cache *cache                                 
    struct objc_protocol_list *protocols                     
#endif

} OBJC2_UNAVAILABLE;

Here a pointer to a struct objc_method_list pointer is declared, which can contain a list of class methods and a list of instance methods

When looking for the address of the IMP, runtime provides two methods

IMP class_getMethodImplementation(Class cls, SEL name); IMP method_getImplementation(Method m)

First method:class_getMethodImplementation

- (void)getIMP_class_getMethodImplementationFromSelector:(SEL)aSelector{
    
    const char *className = object_getClassName([self class]);
     // Get the IMP of the instance
    IMP instanceIMP = class_getMethodImplementation(objc_getClass(className), aSelector);
     // Get the IMP of the class
    IMP classIMP = class_getMethodImplementation(objc_getMetaClass(className), aSelector);
    
    NSLog(@"instanceIMP:%p classIMP:%p",instanceIMP,classIMP);
}

For the first method, both the class and instance methods actually find the IMP address by calling class_getMethodImplementation()

Second method: method_getImplementation

- (void)getIMP_method_getImplementationFromSelector:(SEL)aSelector{
    
    const char *className = object_getClassName([self class]);
     // Get an instance method in a class
    Method instanceMethod = class_getInstanceMethod(objc_getClass(className), aSelector);
     // Get a class method in a class
    Method classMethod = class_getClassMethod(objc_getClass(className), aSelector);
    
     // Get the IMP of the instance
    IMP instanceIMP = method_getImplementation(instanceMethod);
     // Get the IMP of the class
    IMP classIMP = method_getImplementation(classMethod);
    
    NSLog(@"instanceIMP:%p classIMP:%p",instanceIMP,classIMP);
}

In the case of method_getImplementation, the only parameter passed in is method, and the zone classification method and the instance method lie in the function that encapsulates method class method Method class_getClassMethod(Class cls, SEL name) Example method Method class_getInstanceMethod(Class cls, SEL name)

Finally call IMP method_getImplementation(Method m) to get the IMP address

The method list holds the structure of the following method, which contains the implementation of the method. The selector is essentially the name of the method, and the corresponding implementation can be found in the structure by the method name.

struct objc_method {
    SEL method_name                                      
    char *method_types                                       
    IMP method_imp                                           
}

Demo:https://github.com/destinyzhao/SelectorFindIMP


Recommended>>
1、2017 China Autonomous Driving Industry Research Report
2、Big data car checks are coming theres no place for illegal black cars
3、Linked flip book interactive system
4、Happy five how to open cheat assistant happy five hang cheat is real
5、Apple makes a push for blockchain Xunlei Chainke Playcoin explains the security features of distributed technology

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号