IPhoneProgram DevelopmentKVO/KVCImplementation Mechanism analysis is the content to be introduced in this article. Let's take a look at the details.
Key-Value Observing (KVO) Mechanism, very good, can reduce the watering code. AboutKVOLearning, you can refer to the article: "Key-Value Observing Quick Start": http://www.cocoadev.cn/Objective-C/Key-Value-Observing-Quick-Start-cn.asp
Key-Value CodingKVC) Implementation Analysis
KVCIt uses an isa-swizzling technology. Isa-swizzling is a type-based hybrid pointer mechanism.KVCIt mainly uses isa-swizzling to implement internal search and positioning. Isa pointer, as its name indicates, is a kind of), pointing 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.
For example, the following lineKVCCode:
- [site setValue:@"sitename" forKey:@"name"];
It will be processed by the compiler as follows:
- SEL sel = sel_get_uid ("setValue:forKey:");
- IMP method = objc_msg_lookup (site->isa,sel);
- method(site, sel, @"sitename", @"name");
First, we will introduce two basic concepts:
1) SEL data type: it is the environment parameter for the compiler to run the methods in Objective-C.
2) IMP data type: it is actually a function pointer during internal implementation of a compiler. When the Objective-C compiler processes and implements a method, it will point to an IMP object, which is a type expressed in C language. In fact, when the Objective-C compiler processes it, C language ).
For how to find the pointer to implement the function, can refer to the article: Objective-C how to avoid dynamic binding, and get the method address: http://www.cocoadev.cn/Objective-C/Get-method-address.asp
The implementation of KVC is clear: when an object calls setValue, 1) First, find the environment parameters required for running the method based on the method name. 2) he will find the method implementation interface by combining his isa pointer with the environment parameters. 3) directly find the specific method implementation.
Key-Value ObservingKVO) Implementation
Add KVO's automatic observation message and notification mechanism to the KVC mechanism described above.
When the observer registers an object property and the isa pointer of the observed object is modified, the isa Pointer Points to an intermediate class instead of a real class. Therefore, the isa pointer does not need to point to the real class of the Instance Object. Therefore, our program should not depend on the isa pointer. When calling a class method, it is best to specify the Class Name of the object instance.
FamiliarKVOAll of us know that only when we callKVCWhen accessing the key valueKVO. Therefore, it is certain that KVO is implemented based on KVC. As a matter of fact, after reading our analysis above, the idea of the KVO-related architecture is just a matter of course.
BecauseKVCImplementation Mechanism, you can easily seeKVCThe operation Key, and then it is easy to match the Key in the Observer registry. If the accessed Key is the observed Key, we can easily find the observer object in the Observer registry and send a message to it.
Summary:IPhoneProgram DevelopmentKVO/KVCThe implementation mechanism analysis is complete. I hope this article will help you.