Hello! I'm attempting to understand Cocoa's Key-Value Coding (KVC) mechanism a little better. I've read Apple's Key-Value Programming Guide but am still a little confused about how certain KVC methods search for keys. Particularly, mutableArrayValueForKey:.
Below I'm going to explain how I understand valueForKey:
KVC "getters" to work. Then I'll get to my question regarding mutableArrayValueForKey.
There are seven different "getter" KVC methods:
- (id)valueForKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
- (NSMutableArray *)mutableArrayValueForKey:(NSString *)key;
- (NSMutableArray *)mutableArrayValueForKeyPath:(NSString *)keyPath;
- (NSMutableSet *)mutableSetValueForKey:(NSString *)key;
- (NSMutableSet *)mutableSetValueForKeyPath:(NSString *)keyPath;
When searching for a Value inside a Property (named myKey), Apple's docs state that valueForKey: searches like this:
- Tries
-getMyKey
,-myKey
, and-isMyKey
(in that order) inside the receiver If not found, it attempts these ordered, to-many getters (NSArray):
// Required: - (NSUInteger)countOfMyKey; // Requires At Least One: - (id)objectInMyKeyAtIndex:(NSUInteger)index; - (NSArray *)myKeyAtIndexes:(NSIndexSet *)indexes; // Optional (improves performance): - (void)getMyKey:(KeyClass **)buffer range:(NSRange)inRange;
Next, it attempts these unordered, to-many getters (NSSet):
- (NSUInteger)countOfMyKey; - (NSEnumerator *)enumeratorOfMyKey; - (KeyClass *)memberOfMyKey:(KeyClass *)anObject;
Next, it attempts to access Instance Variables directly, assuming
YES
is returned byaccessInstanceVariablesDirectly
, in this order:_myKey
, _isMyKey
,myKey
,isMyKey
.Lastly, it gives up and calls the receiving class's
- (id)valueForUndefinedKey:(NSString *)key
method. Usually an error is raised here.
My question is, what is the search order pattern for mutableArrayValueForKey:?
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:inAtIndex: and -removeObjectFromAtIndex: (corresponding to the NSMutableArray primitive methods insertObject:atIndex: and removeObjectAtIndex: respectively), or methods matching the pattern -insert:atIndexes: and -removeAtIndexes: (corresponding to the NSMutableArrayinsertObjects:atIndexes: and removeObjectsAtIndexes: methods). If at least one insertion method and at least one removal method are found each NSMutableArray message sent to the collection proxy object will result in some combination of -insertObject:inAtIndex:, -removeObjectFromAtIndex:, -insert:atIndexes:, and -removeAtIndexes: messages being sent to the original receiver of mutableArrayValueForKey:. ...etc...
This makes no sense to me as it's discussing "setter" like methods. mutableArrayValueForKey:
returns an NSMutableArray. All of the methods listed above return void, and are used to edit an NSMutableArray, not get it. Example:
- (void)insertMyKey:(KeyClass *)keyObject inMyKeyAtIndex:(NSUInteger)index;
- (void)removeObjectFromMyKeyAtIndex:(NSUInteger)index;
Any idea what Apple is trying to say in their docs, or if this is perhaps an error?
My theory is that mutableArrayValueForKey:
is likely taking a similar path as valueForKey:
when searching to retrieve a KVC value. I'm just not sure what path that really is.
Thanks for any help you can offer! :)