WebObjects 5.4.2

com.webobjects.foundation
Interface NSKeyValueCoding

All Known Subinterfaces:
EOEnterpriseObject, EOKeyValueCoding, EOKeyValueCodingAdditions, NSKeyValueCodingAdditions
All Known Implementing Classes:
BASConfirmPage, BASDefaultHeader, BASEditRelationshipPage, BASErrorPage, BASHead, BASInspectPage, BASListPage, BASMasterDetailPage, BASPlainListPage, BASQueryAllEntitiesPage, BASQueryPage, BASSideHeader, BASSidePageWrapper, D2WActions, D2WCollapsibleComponentContent, D2WCompactInspectComponent, D2WComponent, D2WConfirmPage, D2WContext, D2WCustomComponent, D2WCustomQueryComponent, D2WDefaultCustomComponent, D2WDisplayBoolean, D2WDisplayDate, D2WDisplayHyperlink, D2WDisplayImage, D2WDisplayImageFromPath, D2WDisplayLargeString, D2WDisplayMailTo, D2WDisplayNumber, D2WDisplayString, D2WDisplayStyledDate, D2WDisplayStyledNumber, D2WDisplayStyledString, D2WDisplayThresholdColoredNumber, D2WDisplayToMany, D2WDisplayToMany2, D2WDisplayToManyBrowser, D2WDisplayToManyFault, D2WDisplayToManyTable, D2WDisplayToOne, D2WDisplayToOneFault, D2WEdit, D2WEditBoolean, D2WEditDate, D2WEditLargeString, D2WEditNumber, D2WEditRelationshipPage, D2WEditString, D2WEditToManyFault, D2WEditToManyRelationship, D2WEditToOneFault, D2WEditToOneRelationship, D2WEditUneditable, D2WEmbeddedComponent, D2WEmptyWrapper, D2WErrorPage, D2WGenerationWrapper, D2WHead, D2WInspect, D2WInspectPage, D2WKeyPathContainer, D2WList, D2WListPage, D2WMasterDetailPage, D2WPage, D2WPlainListPage, D2WQuery, D2WQueryAllEntitiesPage, D2WQueryAnyField, D2WQueryBoolean, D2WQueryDateOperator, D2WQueryDateRange, D2WQueryNumberOperator, D2WQueryNumberRange, D2WQueryPage, D2WQueryRangeComponent, D2WQueryRelationship, D2WQueryStringComponent, D2WQueryStringOperator, D2WQueryToManyField, D2WQueryToManyRelationship, D2WQueryToOneField, D2WQueryToOneRelationship, D2WQueryUnavailable, D2WRemoteControl, D2WSelect, D2WStandAloneWebAssistantFrame, D2WStatelessComponent, D2WSwitchComponent, D2WTabInspectPage, D2WWebAssistantFrame, D2WWebAssistantPage, DarwinShadow, DefaultHeader, EditComponent, EditRelationship, EOActionButtonsController, EOActionController, EOActionMenuController, EOActionTrigger, EOActionWidgetController, EOAppletController, EOApplication, EOArchiveController, EOAssociationController, EOBoxController, EOCheckBoxController, EOComboBoxController, EOComponentController, EOController, EOCustomObject, EODefaultActionTrigger, EODetailSelectionController, EODialogController, EODisplayStatisticsController, EODocumentController, EODynamicApplication, EOEditingController, EOEntityController, EOEnumerationController, EOFormatValueController, EOFormController, EOFrameController, EOGenericRecord, EOImageViewController, EOInspectorController, EOInterfaceController, EOListController, EOMenuSwitchController, EOModalDialogController, EOMultipleValuesEnumerationController, EOOneValueEnumerationController, EOProgrammaticSwitchController, EOQueryController, EOQuickTimeViewController, EORangeTextFieldController, EORangeValueController, EORangeWidgetController, EOSchemaColumnDescription, EOSchemaForeignKeyConstraintDescription, EOSchemaIndexDescription, EOSchemaPropertyDescription, EOSchemaSynchronizationChanges, EOSchemaSynchronizationColumnChanges, EOSchemaSynchronizationModelChanges, EOSchemaSynchronizationTableChanges, EOSchemaTableDescription, EOSimpleWindowController, EOSplitController, EOStaticIconController, EOStaticLabelController, EOStaticTextFieldController, EOSwitchController, EOTableColumnController, EOTableController, EOTabSwitchController, EOTextAreaController, EOTextFieldController, EOTitlesController, EOTreeController, EOValueAndURLController, EOValueController, EOWidgetController, EOWindowController, FrontbasePlugIn.FrontbaseSynchronizationFactory.FrontbaseColumnDescription, FrontbasePlugIn.FrontbaseSynchronizationFactory.FrontbaseIndexDescription, FrontbasePlugIn.FrontbaseSynchronizationFactory.FrontbaseTableDescription, JSAlertPanel, JSComponent, JSConfirmPanel, JSImageFlyover, JSModalWindow, JSTextFlyover, JSValidatedField, NEUCompactInspectComponent, NEUConfirmPage, NEUEditRelationshipPage, NEUErrorPage, NEUHeader, NEUInspectPage, NEUListPage, NEUMasterDetailPage, NEUPlainListPage, NEUQueryAllPage, NEUQueryPage, NEUTabInspectPage, NSArray, NSDictionary, NSMutableArray, NSMutableDictionary, OpenBasePlugIn.OpenBaseSynchronizationFactory.OpenBaseColumnDescription, OpenBasePlugIn.OpenBaseSynchronizationFactory.OpenBaseIndexDescription, OpenBasePlugIn.OpenBaseSynchronizationFactory.OpenBaseTableDescription, QueryComponent, QueryOperatorComponent, WOAdminAction, WOAggregateEventRow, WOAnyField, WOApplication, WOAssociationEventRow, WOBatchNavigationBar, WOBeginTransactionOperation, WOCheckboxMatrix, WOCollapsibleComponentContent, WOCommitTransactionOperation, WOCompletionBar, WOComponent, WOContext, WOCookie, WODeleteOperation, WODictionaryRepetition, WODirectAction, WODisplayGroup, WOEventDisplay, WOEventDisplayPage, WOEventPage, WOEventRow, WOEventSetup, WOEventSetupPage, WOExceptionPage, WOFetchSpecSearchOperation, WOHTTPConnection, WOIFrame, WOInsertOperation, WOJavaClientApplet, WOJavaClientComponent, WOJavaClientJNLP, WOKeyValueConditional, WOLCompactInspectComponent, WOLConfirmPage, WOLEditRelationshipPage, WOLErrorPage, WOLHeader, WOLInspectPage, WOLListPage, WOLMasterDetailPage, WOLongResponsePage, WOLPlainListPage, WOLQueryAllPage, WOLQueryPage, WOLTabInspectPage, WOMessage, WOMetaRefresh, WOOperation, WOOperationWSDLTemplate, WOPageRestorationError, WOProjectBuilderAction, WORadioButtonMatrix, WORedirect, WORequest, WOResponse, WORollbackTransactionOperation, WOSearchOperation, WOServletContext, WOSession, WOSessionCreationError, WOSessionRestorationError, WOSimpleArrayDisplay, WOSimpleArrayDisplay2, WOSortOrder, WOSortOrderManyKey, WOStatisticsStore, WOStats, WOStatsPage, WOStringKeyMap, WOTable, WOTableString, WOTabPanel, WOThresholdColoredNumber, WOToManyRelationship, WOToOneRelationship, WOUpdateOperation, WOWSDLGeneration, WOXMLDecoder, WXAccessControl, WXBar, WXOutlineControl, WXOutlineEntry, WXOutlineRepetition, WXSortOrder

public interface NSKeyValueCoding

The NSKeyValueCoding interface defines a data transport 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 takeValueForKey method sets the value for an object's property, and valueForKey returns the value for an object's property.

The NSKeyValueCoding interface contains an inner interface, NSKeyValueCoding.ErrorHandling, that defines an extension to the basic NSKeyValueCoding interface for handling errors that occur during key-value coding.

Additionally, NSKeyValueCoding contains two inner classes, NSKeyValueCoding.DefaultImplementation and NSKeyValueCoding.Utility. The former provides a default implementation of the interface, making it easy to implement NSKeyValueCoding on your's own custom classes. The latter is a convenience that allows you to access the properties of NSKeyValueCoding objects and non-NSKeyValueCoding objects using the same code. Both the DefaultImplementation class and the Utility class provide NSKeyValueCoding.ErrorHandling API in addition to basic NSKeyValueCoding API.

Default Implementation

The methods in the NSKeyValueCoding.DefaultImplementation class are just like the methods defined by the NSKeyValueCoding interface (and the NSKeyValueCoding.ErrorHandling interface), except they are static methods and they take an extra argument -- the object on which the default implementation should operate.

For example, suppose you want to implement an Employee class that implements NSKeyValueCoding using NSKeyValueCoding.DefaultImplementation. Employee's valueForKey method would then look like this:

          <blockquote>

           public Object valueForKey(String key) {
             return NSKeyValueCoding.DefaultImplementation.valueForKey(this, key);
           }
           </blockquote>
 

The NSKeyValueCoding.DefaultImplementation methods use accessor methods normally implemented by objects (such as setKey and key), or they access instance variables directly if no accessors for a key exist.

Note: The default implementations have significant performance optimizations. To benefit from them, NSKeyValueCoding can be implemented on a custom class as shown above by using the methods in NSKeyValueCoding.DefaultImplementation, or if your class inherits from a WebObjects class that implements NSKeyValueCoding, do not override the inherited implementation. Using a custom implementation incurs significant performance penalties.

Utility

Recall that the NSKeyValueCoding.Utility class is a convenience that allows you to access the properties of NSKeyValueCoding objects and non-NSKeyValueCoding objects using the same code.

Utility's methods are similar to DefaultImplementation's methods in that they are static methods and they take an extra argument -- the object on which the method should operate. However, Utility's methods simply check to see if the object on which they operate is an NSKeyValueCoding object and invoke the corresponding NSKeyValueCoding method on the object if it is. Otherwise, they invoke the corresponding DefaultImplementation method, passing the object on which to operate.

For example, suppose that you want to access an object with the NSKeyValueCoding API but you do not know if the object is an NSKeyValueCoding object. To do so, you simply use the corresponding Utility API, as in the following line of code:

          <blockquote>

           theValue = NSKeyValueCoding.Utility.valueForKey(object, key);
           </blockquote>
 

The above line of code is simply a short-cut for the following:

          <blockquote>

           if (object instanceof NSKeyValueCoding) {
             theValue = ((NSKeyValueCoding)object).valueForKey(key);
           } else {
             theValue = NSKeyValueCoding.DefaultImplementation.valueForKey(object, key);
           }
           </blockquote>
 

Directly Accessing Instance Variables

By default, key-value coding methods directly access instance variables if there are no accessor methods for setting and retrieving values. However, you can modify this behavior without overriding the default implementation. To instruct the default implementation to not directly access instance variables, implement the static method canAccessFieldsDirectly on your NSKeyValueCoding class to return false.

canAccessFieldsDirectly

public static boolean canAccessFieldsDirectly()

Returns true if the key-value coding methods can access the corresponding field value directly on finding no accessor method for a property. Returns false if they should not.

An NSKeyValueCoding class does not have to implement this method. It's an optional method that allows a class to tailor key-value coding behavior. By default, the key-value implementation provided by NSKeyValueCoding.DefaultImplementation accesses fields directly if it can not find corresponding accessor methods. An NSKeyValueCoding class can override this behavior by implementing this method to return false, in which case the key-value coding methods will not access fields directly.

This method is not strictly part of this interface because static methods cannot be formally declared in an interface. However, this method is so closely related to the interface as to be considered part of it.

See Also:
NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String), NSKeyValueCoding.valueForKey(java.lang.String), NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.Utility

Nested Class Summary
static class NSKeyValueCoding.DefaultImplementation
          The NSKeyValueCoding.DefaultImplementation class provides the WebObjects default implementations of the NSKeyValueCoding and NSKeyValueCoding.ErrorHandling interfaces.
static interface NSKeyValueCoding.ErrorHandling
          The NSKeyValueCoding.ErrorHandling interface declares an API for handling errors that occur during key-value coding.
static class NSKeyValueCoding.MapImplementation
           
static class NSKeyValueCoding.Null
          NSKeyValueCoding.Null is a final class that defines a unique object used to represent null values in collection objects, such as NSArrays, which do not allow null values.
static class NSKeyValueCoding.UnknownKeyException
          Instances of the NSKeyValueCoding.UnknownKeyException class are created and thrown when an unknown key is encountered during key-value coding.
static class NSKeyValueCoding.Utility
          The NSKeyValueCoding.Utility class is a convenience that allows you to access the properties of NSKeyValueCoding objects and non-NSKeyValueCoding objects using the same code.
static class NSKeyValueCoding.ValueAccessor
          NSKeyValueCoding.ValueAccessor is an abstract class that establishes a mechanism by which NSKeyValueCoding can operate on object's package access instance variables.
 
Field Summary
static NSKeyValueCoding.Null NullValue
          A constant used to represent null in places which do not handle raw null.
 
Method Summary
 void takeValueForKey(Object value, String key)
          Sets the value for the property identified by key to value.
 Object valueForKey(String key)
          Retrieves the value of the property named by key.
 

Field Detail

NullValue

static final NSKeyValueCoding.Null NullValue
A constant used to represent null in places which do not handle raw null.

Method Detail

valueForKey

Object valueForKey(String key)
Retrieves the value of the property named by key.

The default implementation provided by NSKeyValueCoding.DefaultImplementation works as follows:

  1. Searches for a public accessor method based on key. For example, with a key of "lastName", the method looks for a method named getLastName, lastName, or isLastName.
  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", the method looks for a method named _getLastName, _lastName, or _isLastName
  3. If an accessor method is not found and the static method canAccessFieldsDirectly returns true, the method searches for a field based on key and returns its value directly. For the key "lastName", this would be _lastName, _isLastName, lastName, or isLastName.
  4. If neither an accessor method nor a field is found, the method invokes handleQueryWithUnboundKey (defined in NSKeyValueCoding.ErrorHandling).

Note: The default implementations have significant performance optimizations. To benefit from them, implement NSKeyValueCoding on a custom class as shown above by using the methods in NSKeyValueCoding.DefaultImplementation, or if your class inherits from an WebObjects class that implements NSKeyValueCoding, do not override the inherited implementation. Using a custom implementation incurs significant performance penalties.

Parameters:
key - identifies the property to retrieve
Returns:
the value of the property identified by key. Depending on the object you invoke this method upon, null may be replaced with NullValue
Throws:
NSKeyValueCoding.UnknownKeyException - if key cannot be found on the object this method was invoked upon.
See Also:
NSKeyValueCoding.NullValue, NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String), NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.ErrorHandling.handleQueryWithUnboundKey(java.lang.String)

takeValueForKey

void takeValueForKey(Object value,
                     String key)
Sets the value for the property identified by key to value.

The default implementation provided by NSKeyValueCoding.DefaultImplementation works as follows:

  1. Searches for a public accessor method of the form setKey, and invokes it if there is one.
  2. If a public accessor method is not found, searches for a private accessor method of the form _setKey, and invokes it if there is one.
  3. If an accessor method is not found and the static method canAccessFieldsDirectly returns true, searches for a field based on key and sets its value directly. For the key "lastName", this would be _lastName, _isLastName, lastName, or isLastName.
  4. If the type of the value argument is not compatible with the underlying field or method parameter, the default implementation will make an effort to convert between different Java numeric types (Integer, Double, BigDecimal, etc) as well as between Boolean and Number (true = 1, false = 0). If any other conversion would be necessary, an exception is thrown.
  5. If neither an accessor method nor a field is found, it is an error condition. It invokes handleTakeValueForUnboundKey if the object implements NSKeyValueCoding.ErrorHandling or throws NSKeyValueCoding.UnknownKeyException if the object does not.

Note: : The default implementations have significant performance optimizations. To benefit from them, implement NSKeyValueCoding on a custom class as shown above by using the methods in NSKeyValueCoding. DefaultImplementation, or if your class inherits from an WebObjects class that implements NSKeyValueCoding, do not override the inherited implementation. Using a custom implementation incurs significant performance penalties.

Parameters:
value - the new value for the property named by key
key - identifies the property to change
Throws:
NSKeyValueCoding.UnknownKeyException - if key cannot be found on the object this method was invoked upon.
See Also:
NSKeyValueCoding.NullValue, NSKeyValueCoding.valueForKey, NSKeyValueCoding.DefaultImplementation, NSKeyValueCoding.ErrorHandling, NSKeyValueCoding.ErrorHandling.handleTakeValueForUnboundKey(java.lang.Object, java.lang.String)

Last updated June 2008

Copyright © 2000-2008 Apple Inc.