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 getIMP
Using the objective-C runtime functions, then useObjc_msg_lookup (ID, Sel)
On the GNU runtime.
What is an imp? How do I get one?
IMP
Is a C type referring toImplementationOf a method, also known asImplementation pointer. It's a pointer to a function returningID
, AndSelf
AndMethod Selector(Available inside method definitions as the variable_ Cmd
) As the first arguments:
ID (* IMP) (ID, Sel ,...);
With nsobject, you can obtainIMP
For 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.