NSKeyValueCoding

A mechanism by which you can access the properties of an object indirectly by name or key.

Overview

The basic methods for accessing an object’s values are setValue:forKey:, which sets the value for the property identified by the specified key, and valueForKey:, which returns the value for the property identified by the specified key. Thus, all of an object’s properties can be accessed in a consistent manner.

The default implementation relies on the accessor methods normally implemented by objects (or to access instance variables directly if need be).

Topics

Getting Values

- valueForKey:

Returns the value for the property identified by a given key.

- valueForKeyPath:

Returns the value for the derived property identified by a given key path.

- dictionaryWithValuesForKeys:

Returns a dictionary containing the property values identified by each of the keys in a given array.

- valueForUndefinedKey:

Invoked by valueForKey: when it finds no property corresponding to a given key.

- mutableArrayValueForKey:

Returns a mutable array proxy that provides read-write access to an ordered to-many relationship specified by a given key.

- mutableArrayValueForKeyPath:

Returns a mutable array that provides read-write access to the ordered to-many relationship specified by a given key path.

- mutableSetValueForKey:

Returns a mutable set proxy that provides read-write access to the unordered to-many relationship specified by a given key.

- mutableSetValueForKeyPath:

Returns a mutable set that provides read-write access to the unordered to-many relationship specified by a given key path.

- mutableOrderedSetValueForKey:

Returns a mutable ordered set that provides read-write access to the uniquing ordered to-many relationship specified by a given key.

- mutableOrderedSetValueForKeyPath:

Returns a mutable ordered set that provides read-write access to the uniquing ordered to-many relationship specified by a given key path.

Setting Values

- setValue:forKeyPath:

Sets the value for the property identified by a given key path to a given value.

- setValuesForKeysWithDictionary:

Sets properties of the receiver with values from a given dictionary, using its keys to identify the properties.

- setNilValueForKey:

Invoked by setValue:forKey: when it’s given a nil value for a scalar value (such as an int or float).

- setValue:forKey:

Sets the property of the receiver specified by a given key to a given value.

- setValue:forUndefinedKey:

Invoked by setValue:forKey: when it finds no property for a given key.

Changing Default Behavior

accessInstanceVariablesDirectly

Returns a Boolean value that indicates whether the key-value coding methods should access the corresponding instance variable directly on finding no accessor method for a property.

Validation

- validateValue:forKey:error:

Returns a Boolean that indicates whether the value specified by a given pointer is valid for the property identified by a given key.

- validateValue:forKeyPath:error:

Returns a Boolean that indicates whether the value specified by a given pointer is valid for a given key path relative to the receiver.

Deprecated Methods

+ useStoredAccessor

Returns YES if the stored value methods storedValueForKey: and takeStoredValue:forKey: should use private accessor methods in preference to public accessors.

Deprecated
- handleQueryWithUnboundKey:

Invoked by valueForKey: when it finds no property corresponding to key.

Deprecated
- handleTakeValue:forUnboundKey:

Invoked by takeValue:forKey: when it finds no property binding for key.

Deprecated
- storedValueForKey:

Returns the property identified by a given key.

Deprecated
- takeStoredValue:forKey:

Sets the value of the property identified by a given key.

Deprecated
- takeValuesFromDictionary:

Sets properties of the receiver with values from a given dictionary, using its keys to identify the properties

Deprecated
- takeValue:forKeyPath:

Sets the value for the property identified by keyPath to value.

Deprecated
- takeValue:forKey:

Sets the value for the property identified by key to value.

Deprecated
- unableToSetNilForKey:

Invoked if key is represented by a scalar attribute.

Deprecated
- valuesForKeys:

Returns a dictionary containing as keys the property names in keys, with corresponding values being the corresponding property values.

Deprecated

Constants

Key Value Coding Exception Names

This constant defines the name of an exception raised when a key value coding operation fails.

NSUndefinedKeyException userInfo Keys

These constants are keys into an NSUndefinedKeyException userInfo dictionary

NSKeyValueOperator

These constants define the available array operators. See Using Collection Operators for more information.

NSKeyValueValidationError

A key-value coding validation error.

See Also

Object Basics

NSObject

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

NSObject

The group of methods that are fundamental to all Objective-C objects.