analysis on the implementation mechanism of iphone program development KVO/KVC
IPhoneThe realization mechanism analysis of the program development KVO/KVC is the content that this article introduces, does not say much, directly enters the topic. Let's look at the details.
objective-c inside the Key-value observing (KVO) mechanism, very good, can be very good to reduce the watering code. About KVO study, 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 Coding (KVC) Implementation Analysis
KVC used a isa-swizzling technique. Isa-swizzling is the type-mixed pointer mechanism. KVC mainly through the isa-swizzling, to achieve its internal search and positioning. The ISA pointer, as its name implies, (that is, the meaning of the is a kind of), points to the class that maintains the sub-published object. This sub-publication actually contains pointers to the methods in the implementation class, and other data.
For example, the following line KVC code:
[Site setvalue:@ "sitename" forkey:@ "name"];
will be processed by the compiler:
sel sel = Sel_get_uid ("Setvalue:forkey:");
IMP method = Objc_msg_lookup (Site->isa,sel);
method (site, SEL, @ "sitename", @ "name");
First, we introduce two basic concepts:
(1) SEL data type: It is the environment parameter of the method that the compiler runs in Objective-c.
(2) IMP data type: He's actually a function pointer to the compiler's internal implementation. When the Objective-c compiler deals with implementing a method, it points to a Imp object, which is the type of the C language (in fact, in the Objective-c compiler, it is basically the C language).
For information on how to find a pointer to an implementation function, refer to the article: "How to avoid dynamic binding and get method Address" Objective-c: http://www.cocoadev.cn/Objective-C/Get-method-address.asp
The implementation of this KVC is clearly clear : When an object calls SetValue, (1) first finds the environment parameters required to run the method according to the method name. (2) He will combine the environment parameters from his own Isa pointer to find the specific method implemented by the interface. (3) The specific method can be directly found.
key-value Observing (KVO) Implementation
The KVC mechanism described above adds KVO's automatic observation notification mechanism to the system.
when the observer registers the attributes of an object, the ISA pointer points to an intermediate class instead of the real class when the object's ISA pointer is modified. So the ISA pointer does not actually need to point to the real class of the instance object. So our program is best not to rely on the ISA pointer. When invoking a method of a class, it is best to clarify the class name of the object instance.
friends familiar with KVO know that KVO will only work if we call KVC to access the key value. So it is certain that the KVO is based on the KVC implementation. In fact, after looking at our analysis above, the concept of the relationship KVO architecture will be the same.
because of the implementation mechanism of KVC , it is easy to see the key of a KVC operation, and then it is easy to match the key in the Observer registry. If the key being accessed is the observed key, then we can easily find the observer object in the Observer registry and send him a message inside.
Summary:iPhone program Development KVO/KVC Implementation Mechanism analysis of the content of the introduction, I hope this article is helpful to you.
KVC and KVO principles of use:
Yi, KVC (key-value-coding)
1. Set key-value pairs only for class properties
2, set Setvalue:forkey:, that is, Forkey can only be class properties
3, Value Valueforkey
Second, KVO (key-value-observing) When the property of the observer is changed, notify the Observer
1, using KVC to set the class properties
2, Registered observing object AddObserver:forKeyPath:options:context:
3. The Observer class must override the methodObserveValueForKeyPath:ofObject:change:context:
4, application, MVC model, the database (Datamodal) changes, resulting in a view change, in this way to achieve very convenient