What is the KVC
KVC is a mechanism for indirectly accessing properties and relationships of objects using string identifiers. It supports or is related to several mechanisms and techniques that are unique to Cocoa programming, including Core Data, Scriptability, binding techniques, and language features for declaring properties. (Scriptability and binding techniques are specific to OS X). You can also use KVC to simplify your program code.
Key-value coding is a mechanism for indirectly accessing an object’s attributes and relationships using string identifiers. It underpins or is related to several mechanisms and technologies special to Cocoa programming, among them Core Data, application scriptability, the bindings technology, and the language feature of declared properties. (Scriptability and bindings are specific to Cocoa on OS X.) You can also use key-value coding to simplify your program code.
Object properties and KVC
At the heart of KVC lies the general concept of “property”. Properties are the basic unit of object encapsulation and can be of either of the following general types:
- Attributes of the object itself, such as name, title, color, and so on
- Relationships with other objects (references? A pointer? It can be one-to-one or one-to-many
KVC locates the properties of an object by means of a key, which is a string identifier. The key typically corresponds to the name of the accessor method or instance variable defined by the object. The key must conform to certain conventions: it must be ASCII encoded, begin with a lowercase letter, and have no Spaces. The key path is a series of point-separated keys that specify the sequence of object properties to traverse. The attribute of the first key in the sequence is relative to a specific object (Employee1 in the figure below), and each subsequent key is evaluated relative to the value of the previous attribute.
How do I make a class KVC compliant
NSKeyValueCoding is an informal protocol that makes KVC possible. It has two methods:
- valueForKey:
- setValue:forKey:
These two methods are very important. Because they are getting and setting the value of the property through the given key value. NSObject provides a default implementation of these methods, and if a class conforms to KVC, it can rely on this implementation.
How you make a class KVC compliant depends on whether the attribute is an attribute or a one-to-one relationship or a one-to-many relationship.
For attributes and one-to-one relationships, the class must implement at least one of the following in the given priority order (a key refers to the name of the property) :
- The class must have a property named key
- It implements an accessor method called key, and setKey: if the property is mutable. (If the property is a Boolean property, the getter accessor method is of the form isKey)
- It declares the instance variable of form key or _key
Implementing KVC compliance for multiple relationships is a more complex process. Refer to the official documentation: About key-value Coding