function call and send message to object (runtime Understanding)

Source: Internet
Author: User


In writing the code when the gap is actually not to look out, many times it does not matter what is called, many people in order to facilitate understanding, simply called function calls.
This should actually be a feature of OC, message sent.
The specific class
typedef struct objc_class *Class; typedef struct objc_object { Class isa; } *id; typedef struct objc_selector *SEL; typedef id (*IMP)(id, SEL, ...);
Class structure
struct objc_class { **struct** objc_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; ** /*协议链表*/** };
In this methodlists is a list of methods
typedef struct objc_method *Method; typedef struct objc_ method { **SEL method_name;//方法名称** **char *method_types;//方法参数类型** **IMP method_imp;//方法实现的函数指针** };
When you send a message, first go back to its corresponding method list, find it will use the corresponding method.
This is roughly the process, and of course there are a lot of details.
1, it first finds the SEL corresponding method to implement IMP. Because different classes may have different implementations for the same method, the method implementations found depend on the type of the message receiver.
2, then pass the message receiver object (pointer to the message receiver object) and the parameters specified in the method to the method implementation IMP.
3, finally, the return value of the method implementation is returned as the return value of the function. Starain Dou Bean/natewang (Jane book author)
Original link: http://www.jianshu.com/p/ca70bfb142da
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

function call and send message to object (runtime Understanding)

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.