Accessor Search Implementation Details
Key-value coding attempts to use accessor methods to get and set values, before resorting to directly accessing the instance variable. This article describes how the key-value coding methods determine how the value is accessed.
Accessor Search Patterns for Simple Attributes
Default Search Pattern for setValue:forKey:
When the default implementation of setValue:forKey: is invoked for a property the following search pattern is used:
The receiver’s class is searched for an accessor method whose name matches the pattern
set<Key>:.If no accessor is found, and the receiver’s class method
accessInstanceVariablesDirectlyreturnsYES, the receiver is searched for an instance variable whose name matches the pattern_<key>,_is<Key>,<key>, oris<Key>, in that order.If a matching accessor or instance variable is located, it is used to set the value. If necessary, the value is extracted from the object as described in “Representing Non-Object Values.”
If no appropriate accessor or instance variable is found,
setValue:forUndefinedKey:is invoked for the receiver.
Default Search Pattern for valueForKey:
When the default implementation of valueForKey: is invoked on a receiver, the following search pattern is used:
Searches the class of the receiver for an accessor method whose name matches the pattern
get<Key>,<key>, oris<Key>, in that order. If such a method is found it is invoked. If the type of the method's result is an object pointer type the result is simply returned. If the type of the result is one of the scalar types supported byNSNumberconversion is done and anNSNumberis returned. Otherwise, conversion is done and anNSValueis returned. Results of arbitrary types are converted toNSValueobjects, not justNSPoint,NSRange,NSRect, andNSSizetypes).Otherwise (no simple accessor method is found), searches the class of the receiver for methods whose names match the patterns
countOf<Key>andobjectIn<Key>AtIndex:(corresponding to the primitive methods defined by theNSArrayclass) and<key>AtIndexes:(corresponding to theNSArraymethodobjectsAtIndexes:).If the
countOf<Key>method and at least one of the other two possible methods are found, a collection proxy object that responds to allNSArraymethods is returned. EachNSArraymessage sent to the collection proxy object will result in some combination ofcountOf<Key>,objectIn<Key>AtIndex:, and<key>AtIndexes:messages being sent to the original receiver ofvalueForKey:. If the class of the receiver also implements an optional method whose name matches the patternget<Key>:range:that method will be used when appropriate for best performance.Otherwise (no simple accessor method or set of array access methods is found), searches the class of the receiver for a threesome of methods whose names match the patterns
countOf<Key>,enumeratorOf<Key>, andmemberOf<Key>:(corresponding to the primitive methods defined by theNSSetclass).If all three methods are found, a collection proxy object that responds to all
NSSetmethods is returned. EachNSSetmessage sent to the collection proxy object will result in some combination ofcountOf<Key>,enumeratorOf<Key>, andmemberOf<Key>:messages being sent to the original receiver ofvalueForKey:.Otherwise (no simple accessor method or set of collection access methods is found), if the receiver's class method
accessInstanceVariablesDirectlyreturnsYES, the class of the receiver is searched for an instance variable whose name matches the pattern_<key>,_is<Key>,<key>, oris<Key>, in that order. If such an instance variable is found, the value of the instance variable in the receiver is returned. If the type of the result is one of the scalar types supported byNSNumberconversion is done and anNSNumberis returned. Otherwise, conversion is done and anNSValueis returned. Results of arbitrary types are converted toNSValueobjects, not justNSPoint,NSRange,NSRect, andNSSizetypes.If none of the above situations occurs, returns a result the default implementation invokes
valueForUndefinedKey:.
Accessor Search Pattern for Ordered Collections
The default search pattern for mutableArrayValueForKey: is as follows:
The receiver's class is searched for a pair of methods whose names match the patterns
insertObject:in<Key>AtIndex:andremoveObjectFrom<Key>AtIndex:(corresponding to theNSMutableArrayprimitive methodsinsertObject:atIndex:andremoveObjectAtIndex:respectively), or methods matching the patterninsert<Key>:atIndexes:andremove<Key>AtIndexes:(corresponding to theNSMutableArrayinsertObjects:atIndexes:andremoveObjectsAtIndexes:methods).If at least one insertion method and at least one removal method are found each
NSMutableArraymessage sent to the collection proxy object will result in some combination ofinsertObject:in<Key>AtIndex:,removeObjectFrom<Key>AtIndex:,insert<Key>:atIndexes:, andremove<Key>AtIndexes:messages being sent to the original receiver ofmutableArrayValueForKey:.If receiver's class also implements the optional replace object method matching the pattern
replaceObjectIn<Key>AtIndex:withObject:orreplace<Key>AtIndexes:with<Key>:that method will be used when appropriate for best performance.Otherwise, the receiver's class is searched for an accessor method whose name matches the pattern
set<Key>:. If such a method is found eachNSMutableArraymessage sent to the collection proxy object will result in aset<Key>:message being sent to the original receiver ofmutableArrayValueForKey:. It is much more efficient to implement the indexed accessor methods discussed in the previous step.Otherwise, if the receiver's class responds
YEStoaccessInstanceVariablesDirectly, the receiver's class is searched for an instance variable whose name matches the pattern_<key>or<key>, in that order.If such an instance variable is found, each
NSMutableArraymessage sent to the collection proxy object will be forwarded to the instance variable's value, which typically will be an instance ofNSMutableArrayor a subclass ofNSMutableArray.Otherwise, returns a mutable collection proxy object that results in a
setValue:forUndefinedKey:message being sent to the original receiver of themutableArrayValueForKey:message whenever the proxy receives anNSMutableArraymessage.The default implementation of setValue:forUndefinedKey: raises an
NSUndefinedKeyException, but you can override it in your application.
Accessor Search Pattern for Uniquing Ordered Collections
The default implementation of mutableOrderedSetValueForKey: recognizes the same simple accessor methods and ordered set accessor methods as valueForKey (see “Default Search Pattern for valueForKey:”), and follows the same direct instance variable access policies, but always returns a mutable collection proxy object instead of the immutable collection that valueForKey: would return. It also:
Searches the class of the receiver for methods whose names match the patterns
insertObject:in<Key>AtIndex:andremoveObjectFrom<Key>AtIndex:(corresponding to the two most primitive methods defined by theNSMutableOrderedSetclass), and alsoinsert<Key>:atIndexes:andremove<Key>AtIndexes:(corresponding toinsertObjects:atIndexes:] andremoveObjectsAtIndexes:).If at least one insertion method and at least one removal method are found each
NSMutableOrderedSetmessage sent to the collection proxy object will result in some combination ofinsertObject:in<Key>AtIndex:,removeObjectFrom<Key>AtIndex:,insert<Key>:atIndexes:, andremove<Key>AtIndexes:messages being sent to the original receiver ofmutableOrderedSetValueForKey:.If the class of the receiver also implements an optional method whose name matches the pattern
replaceObjectIn<Key>AtIndex:withObject:orreplace<Key>AtIndexes:with<Key>:that method will be used when appropriate for best performance.Otherwise, searches the class of the receiver for an accessor method whose name matches the pattern
set<Key>:. If such a method is found each NSMutableOrderedSet message sent to the collection proxy object will result in aset<Key>:message being sent to the original receiver ofmutableOrderedSetValueForKey:.Otherwise, if the receiver’s class’s
accessInstanceVariablesDirectlymethod returnsYES, searches the class of the receiver for an instance variable whose name matches the pattern _<key> or <key>, in that order. If such an instance variable is found, eachNSMutableOrderedSetmessage sent to the collection proxy object will be forwarded to the instance variable’s value, which therefore must typically be an instance ofNSMutableOrderedSetor of a subclass ofNSMutableOrderedSet.Otherwise, returns a mutable collection proxy object anyway. Each
NSMutableOrderedSetmessage sent to the collection proxy object will result in asetValue:forUndefinedKey:message being sent to the original receiver ofmutableOrderedSetValueForKey:. The default implementation ofsetValue:forUndefinedKey:raises anNSUndefinedKeyException, but you can override it in your application.
Accessor Search Pattern for Unordered Collections
The default search pattern for mutableSetValueForKey: is as follows:
Searches the receiver's class for methods whose names match the patterns
add<Key>Object:andremove<Key>Object:(corresponding to theNSMutableSetprimitive methodsaddObject:andremoveObject:respectively) and alsoadd<Key>:andremove<Key>:(corresponding toNSMutableSetmethodsunionSet:andminusSet:). If at least one addition method and at least one removal method are found eachNSMutableSetmessage sent to the collection proxy object will result in some combination ofadd<Key>Object:,remove<Key>Object:,add<Key>:, andremove<Key>:messages being sent to the original receiver ofmutableSetValueForKey:.If the class of the receiver also implements an optional method whose name matches the pattern
intersect<Key>:orset<Key>:, that method will be used when appropriate for best performance.If the receiver is a managed object, the search pattern does not continue as it would for non-managed objects. See “Managed Object Accessor Methods” in Core Data Programming Guide for more information.
Otherwise, the receiver's class is searched for an accessor method whose name matches the pattern
set<Key>:. If such a method is found, eachNSMutableSetmessage sent to the collection proxy object will result in aset<Key>:message being sent to the original receiver ofmutableSetValueForKey:.Otherwise, if the receiver's class method
accessInstanceVariablesDirectlyreturnsYES, the class is searched for an instance variable whose name matches the pattern_<key>or<key>, in that order. If such an instance variable is found, eachNSMutableSetmessage sent to the collection proxy object will be forwarded to the instance variable's value, which therefore must typically be an instance ofNSMutableSetor a subclass ofNSMutableSet.Otherwise, returns a mutable collection proxy object anyway. Each
NSMutableSetmessage sent to the collection proxy object will result in asetValue:forUndefinedKey:message being sent to the original receiver ofmutableSetValueForKey:.
© 2003, 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-07-17)