IOS KVC KVO KVB

Source: Internet
Author: User

KVC uses an ISA-swizzling technology. Isa-swizzling is a type-based hybrid pointer mechanism. KVC mainly uses Isa-swizzling to implement internal search and positioning. The ISA pointer, as its name implies (that is, a kind of), 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.

For exampleCode:

[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 within the compiler. When the objective-C compiler implements a method, it points to an imp object, which is a type expressed in C Language (in fact, basically, the objective-C compiler processes the objective-C language ).

The implementation of KVC is clear: when an object calls setvalue, (1) first, find the required environmental parameter number for running the method based on the method name. (2) he will use his ISA pointer to combine environment parameters to find the specific method to implement the interface. (3) directly find the specific method implementation.

Overview of the key-value observing Mechanism

Key-value observing (KVO): This mechanism allows an object to receive notifications when the attributes of the specified object are modified. Each time the attribute of the specified observed object is modified, KVO automatically notifies the corresponding observer.

Advantages of KVO

When attributes change, KVO will provide automatic message notifications. This architecture has many advantages. First, developers do not need to implement this solution by themselves: A Message notification is sent every time the attribute changes. This is the biggest advantage of the KVO mechanism. This solution has been clearly defined and can be easily used with framework-level support. Developers do not need to add any code or design their own observer model, which can be used directly in the project. Secondly, the KVO architecture is very powerful, and it can easily support multiple observers to observe the same attribute and related values.


KVB
two basic methods
1: add the observer for the object
addobserver: forkeypath: Options: context:

2: function used by the observer to receive information
observevalueforkeypath: ofobject: change: context:


the MVC Architecture is "Model-View-controller ". "abbreviation, the Chinese translation is "Mode-View-controller ". The MVC application Program is always composed of these three parts. Event causes the Controller to change the model or view, or change both. As long as the controller changes the data or attributes of models, all dependent views are automatically updated. Similarly, as long as the controller changes the view, the view will retrieve data from the potential model to refresh itself.

The MVC Architecture was first proposed by the Smalltalk Language Research Group and applied to user interaction applications.

The MVC pattern is a complex architecture pattern, and its implementation is also very complicated. However, we have summarized a lot of reliable design patterns. The combination of multiple design patterns makes the implementation of the MVC pattern relatively simple. Views can be seen as a tree, which can be implemented by composite pattern. The relationship between views and models can be reflected by observer pattern. The controller controls the display of views, which can be implemented using strategy pattern. The model is usually a mediator and mediator can be used.
Pattern.


KVC--KVO--KVB advantages
Some mechanisms define a set of common cocoa naming rules and calling rules to implement the following functions:

1.Use a highly normalized access method to obtain and set the value of any attribute of any object (the so-called attribute can be a real member variable, it can also be a property of the object abstracted by a pair of member methods ). 2.By inheriting a specific method and specifying the object to be monitored and the name of the attribute to be monitored, you can change the value of the specified attribute of the object, get a "notification" (although this is not a true notification) and get a change in the value of the relevant attribute (the original value and the new value after the change ). 3.A simple function call ensures that a specified attribute of a view object is synchronized with a specified attribute of a controller object or model object anytime, anywhere.

In many cases, there areKVC, KVOBut all of them are included..Only know that this isObject-CProvides a good mechanism to reduce watering code.

 

ActuallyKVC,KVOThat isNskeyvaluecodingAndNskeyvaluecoding.

As described in the official document

 

Then weKVO,KVCHow can we use it for something?

 

First, let's get to knowKVOMechanism

KVO:When the attribute of the specified object is modified, the object is allowed to receive notifications. Each time a listener is defined in a class

For example:

[SelfAddobserver:Self

 Forkeypath:@ "Items"

 Options:0

 Context: contexstr];

***

Of course, you can also monitor the attribute changes of other objects.

[Person addobserver: Money

 Forkeypath:@ "Account"

 Options:0

 Context: contexstr];

****

As long as the current class isItemsChanges to this attribute will trigger the following methods.

-(Void) Observevalueforkeypath :( nsstring *) keypath

Ofobject :(ID) Object

Change :( nsdictionary *) Change

Context :(Void*) Context

 

KVOBenefits:

When attributes change,KVOAutomatic Message notifications are provided. In this way, developers do not need to implement the solution by themselves: Send a message Notification every time the attribute changes.

This isKVOThe mechanism provides the biggest advantages. This solution has been clearly defined and can be easily used with framework-level support.

Developers do not need to add any code or design their own observer models, which can be used directly in the project.

Second,KVOThe architecture is very powerful, and it is easy to support multiple observers to observe the same attribute and related values.

 

KVCImplementation Analysis

KVCUsingIsa-swizzlingTechnology.

Isa-swizzlingIs the Type Hybrid pointer mechanism.KVCMainly throughIsa-swizzlingTo implement internal search and positioning.

IsaPointer,YesIs
A kindMeaning,The class that points to the object that maintains the sub-table. The sub-Table actually contains pointers to methods in the implementation class, and other data.

As follows:KVCCode:

[Person setvalue:@ "Personname"Forkey:@ "Name"];

 

It will be processed by the compiler as follows:

SelSel = sel_get_uid ("Setvalue: forkey :");

IMPMethod = objc_msg_lookup (person-> Isa, Sel );

Method (person, Sel,@ "Personname",@ "Name");

***

Where:

SelData Type: it is run by the compilerObjective-CEnvironment Parameters of the method.

IMPData Type: it is actually a function pointer during internal implementation of the compiler. WhenObjective-CWhen the compiler implements a method, it points toIMPObject, which isCLanguage Expression type.

***

KVCIn the call MethodSetvalueTime

(1First, find the environment parameters required for running the method based on the method name.

(2) From his ownIsaThe pointer works with environment parameters to find the interface implemented in a specific method.

(3.

 

In this case,KVOImplementation is easy to understand

When an object registers an observer,Of the observed objectIsaWhen the pointer is modified, IsaThe Pointer Points to an intermediate class instead of a real class.

SoIsaThe pointer does not need to point to the real class of the Instance Object. So our program should not depend onIsaPointer. When calling a class method, it is best to specify the Class Name of the object instance.

In this way, only when we callKVCAccessKeyValueKVO. So surely,KVOIs based onKVC.

Related Article

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.