Next Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

NSKeyValueCoding

(informal protocol)

Package
com.apple.cocoa.foundation
Companion guide

Overview

The NSKeyValueCoding interface defines a mechanism in which the properties of an object are accessed 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.

The basic method for accessing an object’s value is valueForKey, 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).

Tasks

Getting Values

Setting Values

Instance Methods

takeValueForKey

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

public abstract void takeValueForKey(Object value, String key)

Discussion

The default implementation works as follows:

  1. Searches for a public accessor method of the form setKey, invoking it if there is one.

  2. If a public accessor method is not found, searches for a private accessor method of the form _setKey, invoking it if there is one.

  3. If an accessor method is not found takeValueForKey searches for an instance variable based on key and sets the value directly. For the key “lastName”, this would be _lastName or lastName.

valueForKey

Returns the value for the property identified by key.

public abstract Object valueForKey(String key)

Discussion

The default implementation works as follows:

  1. Searches for a public accessor method based on key. For example, with a key of “lastName”, valueForKey looks for a method named getLastName or lastName.

  2. If a public accessor method is not found, searches for a private accessor method based on key (a method preceded by an underbar). For example, with a key of “lastName”, valueForKey looks for a method named _getLastName or _lastName.

  3. If an accessor method is not found valueForKey searches for an instance variable based on key and returns its value directly. For the key “lastName”, this would be _lastName or lastName.

Constants

These constants define the available array operators. See “Set and Array Operators” for more information.



Next Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.