The difference between KVC and KVO in iOS

Source: Internet
Author: User
Tags notification center
first, KVC and Kvo
* "KVC": Key value Coding (key code) * Objective: To indirectly modify or get the properties of the object, reducing the coupling between the program (class and Class).
* "KVO": Key value Observer (observed by the keys), observer mode. * Purpose: Often used to observe a change in a property of an object in time to make a corresponding.
second, the use of the way KVC

KVC is called the iOS development platform's big recruit!!! Can quickly modify object properties. *[p1setvalue:@ "xxxx" forkeypath:@ "name"; Modify the specified object properties.  *[arrvalueforkeypath:@ "Book.bookname"]; Gets the object property. *KVC can get the array directly through the key. * When using KVC, be sure to ensure that the key value exists. * Principle: When a value is KVC, if the object is found to not contain the specified key value, it automatically goes to the inner member of the object to find it.
third, the use of KVO
*KVO is often used to observe the change in an object's properties and react accordingly. (much used to observe model data) a distinction between *KVO and notification centers: KVO can only stare at an object (working within the current area), and the notification Center can traverse many layers (such as Viewcontroller). *kvo use steps: *1. [stuaddobserver:selfforkeypath:@ ' Name ' options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:@ "Myobserver"];  *2.observevalueforkeypath. This method is automatically invoked when the specified key value changes *3.removeobserver. All observer modes have poor performance and need to be removed in a timely manner.
Directly on the code:[OBJC]  View plain copy//  //  xnviewcontroller.m  //  KVC----KVO   //  //  created by neng on 14-6-21.  //   copyright  (c)  2014 year  neng. All rights reserved.  //       #import   "XNViewController.h"    #import   "XNPerson.h"    #import   XNStudent.h "   #import  " XNBook.h "      @interface  XNViewController  ( )       @end   /**   *  kvc: key value coding (key value code    *   indirectly modify/Get the object's properties,  reduce the coupling between classes and classes .   *  kvo: key value  observer (Key-value observation)      KVO are often used to observe a change in the "object's properties" and respond in a timely manner.       Nsnotificationcenter is an object that requires the post "notification string" (which indicates the type of event being monitored) to be present, and the notification Center can work.    */      @implementation  xnviewcontroller     -  (void) viewdidload {       [ super viewdidload];       //1. Simple Modify Object Properties        [ self kvcdemo1];       //2. For subclass,  can also directly modify         [self kvcDemo2];       //3. Modify the book property of an object .  (rather troublesome,  to create a book object). KVC word        [self kvcDemo3];       //4. Gets the property value of the object .  can be obtained in batches (if it is an array) .       [self kvcDemo4];           //KVO  Demo        [self kvoDemo];   }     

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.