NSKeyValueCoding

The NSKeyValueCoding informal protocol defines a mechanism by which you can access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables. Thus, all of an object’s properties can be accessed in a consistent manner.

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. The default implementation uses the accessor methods normally implemented by objects (or to access instance variables directly if need be).

Symbols

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)

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

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

Returns a Boolean value that indicates whether the value specified by a given pointer is 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.

Array operators

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