IOS--DAY05---KVC,KVO

Source: Internet
Author: User

Source: http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html

Students familiar with OC Grammar may know this: in OC, the member variables or methods of a class are not absolutely private.

Private methods cannot be accessed directly through class instances, but can be aided by the "compile-time" mechanism of OC, also known as the "Blind Darkness" mechanism (personal understanding: As long as the class has a method A, you are privately owned, I can call you with the Performselector function), speaking of which, perhaps a reunion naturally remembered , how does the private variable access it? Seems to have never done so before, but the reality is can, but generally we put the variable as a private variable of the class is not want to own or other people to visit, or I can only say that you are looking for a cheap ...

Well, to get to the bottom, this article mainly wants to introduce the implementation mechanism of KVC, KVO and KVB in the next OC, which is certainly related to what I said above.

1. Kvc:key-value Coding (key value code)

It is a mechanism for using string identifiers to indirectly access object properties, which is the basis for many techniques. The main methods are two pairs of methods: (Setvalue:forkey,valueforkey), setvalue:forkeypath,valueforkeypath); What does this thing do, I don't say the principle first, how to use it, the example is as follows:

@interface A {nsstring* foo;}
...//other code

@interface B {nsstring* bar; A * MyA; }



...//Assume an object of type A, type
b * A = ...;
b* B = ...;
nsstring* S1 = [a valueforkey:@ "foo"]; That's right
nsstring* s2 = [b valueforkey:@ "Bar"]; That's right
nsstring* s3 = [b valueforkey:@ "MyA"]; That's right
nsstring* S4 = [b valueforkeypath:@ "Mya.foo"]; That's right
nsstring* S5 = [b valueforkey:@ "Mya.foo"]; Error
nsstring* s6 = [b valueforkeypath:@ "Bar"]; That's right

@end

In fact, the two pairs of methods above are basically the same, but the Valueforkeypath value is a path (between the paths with a dot number). segmentation), such as the data member is the object itself, the value-seeking process will go down deep down.
Note that the names of the data members here are in the form of strings used. The best use of this method is to bind the data (first name) to a number of triggers (especially method calls).
For example, key-value pairs are observed (Key-value observing, KVO) and so on.

The above code shows that the class member variable can also use the base class NSObject the two pairs of methods to access, not necessarily directly through the class instance access, but this way still has a certain risk, the specific danger situation please refer to this: http://www.devbean.info/2011/04/ from_cpp_to_objc_20/
Then I say the principle, is I copy come over, we look at the following:
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).
The implementation of this KVC is clearly clear: when an object calls SetValue,
(1) The environment parameters needed to run the method are first found 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.

2.kvo:key-value Observing (key-value listener) and Kvb:key-value binding (key-value binding)

These two mechanisms are used together, respectively,

Key-value Observing (abbreviated as KVO): Allows the object to accept the notification mechanism when the specified object's properties have been modified. Each time the specified object's properties are modified, the KVO automatically notifies the appropriate observer.

Advantages of KVO

When there is a property change, KVO will provide an automatic message notification. Such an architecture has many benefits. First, developers do not need to implement the scenario themselves: send a message notification each time the property changes. This is the greatest advantage provided by the KVO mechanism. Since the scheme has been clearly defined, it can be easily adopted with framework-level support. Developers do not need to add any code, they do not need to design their own observer model, directly can be used in the project. Second, the KVO architecture is so powerful that it's easy to support multiple observers observing the same property, as well as related values.

Two basic methods of KVB implementation
1: Add observer observer AddObserver:forKeyPath:options:context to the object:
2: Observer observer receives the message processing function ObserveValueForKeyPath:ofObject:change:context:

KVO and KVB The most obvious use of the scene is in some of the interface real-time display line is strong, such as stock trend, ticket balance, this way to avoid the trouble of our own operation notice, think of this, I found that the original point of gold and 91 of the Market download page Progress display can also be used this way.

Let's talk about it first, more wonderful to be continued ...

Original English Link: http://maniacdev.com/2012/12/helper-classes-for-easy-cocoa-touch-key-value-observing-kvo-and-key-value-binding-kvb/

1, Setvalue:forkey: can access to setxxx: method.

2. Valueforkey: You can access the instance variables and methods without parameters.

3, Performselector can access all methods.

IOS--DAY05---KVC,KVO

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.