<!--
{
  "documentType" : "article",
  "framework" : "ObjectiveC",
  "identifier" : "/documentation/ObjectiveC/nskeyvaluecoding",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "NSKeyValueCoding"
}
-->

# NSKeyValueCoding

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

## Discussion

The basic methods for accessing an object’s values are [`setValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKey:)), which sets the value for the property identified by the specified key, and [`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:)), 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

[`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:))

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

[`value(forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKeyPath:))

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

[`dictionaryWithValues(forKeys:)`](/documentation/ObjectiveC/NSObject-swift.class/dictionaryWithValues(forKeys:))

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

[`value(forUndefinedKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forUndefinedKey:))

Invoked by [`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:)) when it finds no property corresponding to a given key.

[`mutableArrayValue(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableArrayValue(forKey:))

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

[`mutableArrayValue(forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableArrayValue(forKeyPath:))

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

[`mutableSetValue(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableSetValue(forKey:))

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

[`mutableSetValue(forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableSetValue(forKeyPath:))

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

[`mutableOrderedSetValue(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableOrderedSetValue(forKey:))

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

[`mutableOrderedSetValue(forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/mutableOrderedSetValue(forKeyPath:))

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:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKeyPath:))

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

[`setValuesForKeys(_:)`](/documentation/ObjectiveC/NSObject-swift.class/setValuesForKeys(_:))

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

[`setNilValueForKey(_:)`](/documentation/ObjectiveC/NSObject-swift.class/setNilValueForKey(_:))

Invoked by [`setValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKey:)) when it’s given a `nil` value for a scalar value (such as an `int` or `float`).

[`setValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKey:))

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

[`setValue(_:forUndefinedKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forUndefinedKey:))

Invoked by [`setValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/setValue(_:forKey:)) when it finds no property for a given key.

### Changing Default Behavior

[`accessInstanceVariablesDirectly`](/documentation/ObjectiveC/NSObject-swift.class/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:)`](/documentation/ObjectiveC/NSObject-swift.class/validateValue(_:forKey:))

Indicates whether the value specified by a given pointer is valid, or can be made valid, for the property identified by a given key.

[`validateValue(_:forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/validateValue(_:forKeyPath:))

Indicates whether the value specified by a given pointer is not valid for a given key path relative to the receiver.

### Deprecated Methods

[`useStoredAccessor()`](/documentation/ObjectiveC/NSObject-swift.class/useStoredAccessor())

Returns `true` if the stored value methods [`storedValue(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/storedValue(forKey:)) and [`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:)) should use private accessor methods in preference to public accessors.

[`handleQuery(withUnboundKey:)`](/documentation/ObjectiveC/NSObject-swift.class/handleQuery(withUnboundKey:))

Invoked by [`value(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/value(forKey:)) when it finds no property corresponding to `key`.

[`handleTakeValue(_:forUnboundKey:)`](/documentation/ObjectiveC/NSObject-swift.class/handleTakeValue(_:forUnboundKey:))

Invoked by [`takeValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValue(_:forKey:)) when it finds no property binding for `key`.

[`storedValue(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/storedValue(forKey:))

Returns the property identified by a given key.

[`takeStoredValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeStoredValue(_:forKey:))

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

[`takeValues(from:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValues(from:))

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

[`takeValue(_:forKeyPath:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValue(_:forKeyPath:))

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

[`takeValue(_:forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/takeValue(_:forKey:))

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

[`unableToSetNil(forKey:)`](/documentation/ObjectiveC/NSObject-swift.class/unableToSetNil(forKey:))

Invoked if `key` is represented by a scalar attribute.

[`values(forKeys:)`](/documentation/ObjectiveC/NSObject-swift.class/values(forKeys:))

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

### Constants

[Key Value Coding Exception Names](/documentation/ObjectiveC/key-value-coding-exception-names)

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

[NSUndefinedKeyException userInfo Keys](/documentation/ObjectiveC/nsundefinedkeyexception-userinfo-keys)

These constants are keys into an `NSUndefinedKeyException` `userInfo` dictionary

  <doc://com.apple.documentation/documentation/Foundation/NSKeyValueOperator>

  <doc://com.apple.documentation/documentation/Foundation/NSKeyValueValidationError-swift.var>

## See Also

  [Key-Value Coding Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107i)



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)