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 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

func value(forKey: String)

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

func value(forKeyPath: String)

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

func dictionaryWithValues(forKeys: [String])

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

func value(forUndefinedKey: String)

Invoked by value(forKey:) when it finds no property corresponding to a given key.

func mutableArrayValue(forKey: String)

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

func mutableArrayValue(forKeyPath: String)

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

func mutableSetValue(forKey: String)

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

func mutableSetValue(forKeyPath: String)

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

func mutableOrderedSetValue(forKey: String)

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

func mutableOrderedSetValue(forKeyPath: String)

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

func setValue(Any?, forKeyPath: String)

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

func setValuesForKeys([String : Any])

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

func setNilValueForKey(String)

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

func setValue(Any?, forKey: String)

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

func setValue(Any?, forUndefinedKey: String)

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

Validation

func validateValue(AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey: String)

Throws an error when the value specified by a given pointer is not valid or can't be made valid for the property identified by a given key.

func validateValue(AutoreleasingUnsafeMutablePointer<AnyObject?>, forKeyPath: String)

Throws an error when the value specified by a given pointer is not valid for a given key path relative to the receiver.

Constants

Key Value Coding Exception Names

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

struct NSKeyValueOperator

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

var NSKeyValueValidationError: Int

Key-value coding validation error.

See Also

Object Basics

class 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.

protocol NSObjectProtocol

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