The implementation mechanism of KVO. Just explain Isa-swizzling.

Source: Internet
Author: User

Isa-swizzling

 

Isa, is a kind

Swizzling, mix, and mix.

 

Basic KVC of KVO.

 

KVC is mainly implemented through Isa-swizzling.

 

[Site setvalue: @ "sitename" forkey: @ "name"];

 

The compiler processes the following:

 

Sel sel = sel_get_uid ("setvalue: forkey :");
IMP method = objc_msg_lookup (site-> Isa, Sel );
Method (site, Sel, @ "sitename", @ "name ");

 

Selectors

Selectors are the run-time system's identifier for a method.SelData type is used for selectors.

TheSel_get_uid ()Function can be used to get a method's selector from it's name:

 

Objc_msg_lookup

If we want to getIMPUsing the objective-C runtime functions, then useObjc_msg_lookup (ID, Sel)On the GNU runtime.

What is an imp? How do I get one?

IMPIs a C type referring toImplementationOf a method, also known asImplementation pointer. It's a pointer to a function returningID, AndSelfAndMethod Selector(Available inside method definitions as the variable_ Cmd) As the first arguments:

 
ID (* IMP) (ID, Sel ,...);

With nsobject, you can obtainIMPFor a given method by doing:

 
IMP imp = [OBJ methodforselector: @ selector (Message)];

For object, do:

IMP imp = [OBJ methodfor: @ selector (Message)];
 
How do I send a message given an imp?

Dereference it, as with a C function pointer:

 
Id anobject, theresult; imp someimp; SEL aselector; //... theresult = someimp (anobject, aselector );

========================

When an observer is registered for an attribute of an object the ISA pointer of the observed object is modified, pointing to an intermediate class rather than at the true class. as a result the value of the ISA pointer does not necessarily reflect the actual class of the instance.

Instead of relying on the ISA pointer your application shocould use the class method to determine the class of an object instance.

Key-value observation Implementation Details
Automatic Key-value observation is implemented by ISA-swizzling.

The ISA pointer, as its name indicates, points to the class of the object that maintains the sub-table. The sub-Table actually contains pointers to methods in the implementation class, and other data.

When an object property registers an observer, the ISA pointer of the observed object is changed to an intermediate class instead of a real class. Therefore, the value of the ISA pointer does not necessarily reflect the actual class of the instance.

YourProgramThe class method should be used to determine the class of the Instance Object, rather than relying on the ISA pointer.

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.