Analysis on implementation mechanism of KVO/KVC in iPhone Program Development

Source: Internet
Author: User

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:

 
 
  1. [site setValue:@"sitename" forKey:@"name"]; 

It will be processed by the compiler as follows:

 
 
  1. SEL sel = sel_get_uid ("setValue:forKey:");  
  2. IMP method = objc_msg_lookup (site->isa,sel);  
  3. 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.

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.