Simple summary of IOS KVC and KVO differences

Source: Internet
Author: User

KVC:

Key value coding. Is a method that accesses an object's properties indirectly by using the name of the property (key). This method does not use the Setter/getter method to access the properties of the object. The essence of this method is to use Valueforkey instead of getter methods, Setvalueforkey instead of setter methods.

Common methods of KVC:

-(ID) Valueforkey: Read the properties of an object with a key value

-(ID) Setvalue:forkey: Using the value of the key to use the property

-(ID) Setnilvalueforkey: Sets the value of the given key word to nil

-(ID) Setvalueforundefinedkey: Error interception, called when KVC does not find a key value.

Features of KVC:

    • You can either take a value or assign a value.
    • KVC is a kind of indirect transfer value, which is advantageous to the solution coupling.
    • KVC can assign a value to a read-only property/private property
    • The KVC has the function of automatic boxing (automatic type conversion).
    • Support for key-value paths

Key-Value path: Plainly, it is the value of a key that is deeply nested. The difference between the two methods is mainly manifested in Setvalue:forkey: and Setvalue:forkeypath:

Setvalue:forkey: Method, when used only in this layer to find the key value to assign

Setvalue:forkeypath: When used, the method is searched for throughout the project according to the key value passed in. It is assigned a value until the key value is found.

    • KVC provides functions such as Avg,sum,max,min,count, which can be used directly

Disadvantages of KVC

    • Because KVC is accessed through a key string, the compiler cannot detect the correctness of the given key value.
    • KVC's execution efficiency is lower than setter and getter methods, because KVC to parse the given key value before accessing the corresponding property.

The implementation principle of KVC:

KVC first based on the incoming AnyKey (take this key as an example) to find out whether the object contains AnyKey method, found in the direct use, if not found to continue to find whether the property contains AnyKey, find the words directly use, if not found, then try to call Setvalueforundefinekey, if the method is not implemented, it will be an error.

Note: KVC looks for methods and properties of AnyKey, not only looking for AnyKey, but also finding _AnyKey, Getanykey and _getanykey

KVO:

Key value observing, the keys are listening, the popular point is that when the specified object's properties change, the object will be notified. That is, each time the specified object changes, the listener receives the message.

KVO Principle of Use:

KVO: Key-value monitoring. It is the observer pattern implemented by the cocoa Framework, which is used in conjunction with KVC. A change in value can be monitored by KVO. is a one-to-many relationship where a value has changed, notifying all observers.

How to use KVO:

    • Registered
    • Use the callback method.
    • Removing observers

Simple summary of IOS KVC and KVO differences

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.