Key-value coding lets you access the properties of an object (such as the instance variables) indirectly by using strings referred to as keys . Although key-value coding can access instance variables directly, it first tries to use accessor methods to access a property. However, accessor methods are not necessarily mapped to instance variables, which means that an accessor may provide a property value that is computed (perhaps from instance variables).
Key-value coding is a powerful feature of Cocoa that forms the basis of many important technologies. For example, Cocoa’s scripting capability is heavily based on the functionality of key-value coding.
The methods that provide an interface to key-value coding are
declared in the Foundation framework’s
protocol. The principal methods are
takeValue:forKey:, which get and set the
instance variable associated with the specified key.
NSObject provides default implementations of the
NSKeyValueCoding. These default
implementations associate keys with instance variables based on a
simple set of rules. The methods that return a value,
valueForKey: for instance, attempt to access the
property specified by the string
@"key" using the
A public accessor of either the form
A private accessor method of either the form
An instance variable named either
Finally, if none of these first three attempts results in anything, the method ...