Introduction to KVO and KVC

Source: Internet
Author: User
I. KEY-VALUE-OBSERVING--KVO (key observer mode) (1) Introduction: This mechanism can listen to the property changes of another class in one class, including our own class and the classes in the existing Cocoa class library. When the properties of the listening class change, the observer receives a notification that it can add its own action for the property change. (2) Register the observer for the listener: Specify the Observer, the name of the property to observe, and the options can pass 0 values. –addobserver:forkeypath:options:context: (3) The observer realizes the notification receive function: –observevalueforkeypath:ofobject:change:context: (4) Remove the observer before the observer or the Observer is destroyed: –removeobserver:forkeypath:context: (5) when the corresponding key value of the object being monitored changes, the observer receives notification, such as assigning a value directly to the attribute, The latter invokes the-setvalue:forkey: method.
Second, KVO notification trigger mode: (1) in the Observer class to implement + Automaticallynotifiesobserversforkey: method, when the return yes, the key will automatically trigger the notification when the value changes, if you return no, you need to trigger the notification manually, Before and after the value changes add the following method: –willchangevalueforkey:–didchangevalueforkey: (2) is automatic or manual, according to the actual situation, the default is automatic. (3) When the direct assignment, to add the object, such as the current object of the property change to add self, otherwise do not trigger notifications, need to manually trigger.
Third, KVO dependency key

Sometimes the value of a property depends on other property values, so if other property values are changed, it is inevitable that the value of the property changes, that is, Dependent poroperties. In Kvo, a dependency key is introduced

In Kvo, there are two ways to implement a dependency key: [CPP] view plain copy + (Nsset *) Keypathsforvaluesaffectingvalueforkey: (NSString *) key + (Nsset *) keypathsforvaluesaffecting<key> For example, the information attribute relies on the age and grade properties of target, and Target's age/gr Any change in the ADE attribute will be notified to information observers.

[CPP] view plain copy   +  (nsset *) keypathsforvaluesaffectinginformation      {         NSSet * keyPaths = [NSSet  setwithobjects:@ "Target.age",  @ "Target.grade", nil];          return keypaths;    }          +  (NSSet  *) Keypathsforvaluesaffectingvalueforkey: (nsstring *) key     {          NSSet * keyPaths = [super  keypathsforvaluesaffectingvalueforkey:key];         nsarray *  moreKeyPaths = nil;                  if  ([key isequaltostring:@ "Information"])           {             morekeypaths = [nsarray arraywithobjects:@ " Target.age ",  @" Target.grade ", nil];         }                   if  (morekeypaths)          {              keyPaths = [keyPaths setByAddingObjectsFromArray:moreKeyPaths];          }                   return keyPaths;    }    to implement   KeypathsforvaluesaffectingInformationOr Keypathsforvaluesaffectingvalueforkey: The method tells the system what other properties the information property depends on, both of which return a collection of Key-path.


If you choose to implement Keypathsforvaluesaffectingvalueforkey, you first get the result set of Super return, and then determine if the key is the target key, and append the Key-path of the dependent property to the knot returned by the super. Fruit set, otherwise directly returns the result of super.

Note: Obviously the former is simple to achieve.
Iv. KEY-VALUE-CODING--KVC (Key-value encoding) (1) This mechanism will treat the properties of the class as a key.     (2) Take value: [Object valueforkey:@ "key"]; Set Value: [Object setvalue:@ "value" forkey:@ "key"]; (3) KVO is implemented based on KVC.

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.