Doesn't -sortedArrayUsingDescriptors: use KVC accessors?
Doesn't -sortedArrayUsingDescriptors: use KVC accessors?
- Subject: Doesn't -sortedArrayUsingDescriptors: use KVC accessors?
- From: Jerry Krinock <email@hidden>
- Date: Mon, 20 Mar 2006 13:54:37 -0800
- Thread-topic: Doesn't -sortedArrayUsingDescriptors: use KVC accessors?
I'm trying to use NSArray -sortedArrayUsingDescriptors: to sort an array of
NSMutableDictionaries. I read that "the first descriptor specifies the
primary key value path to be used in sorting". It's a little vague, but I
expect this method to use the four-step valueForKey: search pattern
described in the "Key-Value Coding Programming Guide" (*).
Am I interpreting the documentation correctly?
Instead, it seems to be ignoring my accessors (step #1 of 4) and instead
using NSDictionary -objectForKey:.
CODE
I've added a category to NSMutableDictionary which contains two types of
"accessors":
1. "Simple Accessors" which simply return NSDictionary -objectForKey:, for
example:
- (NSString*)name {
return [self objectForKey:@"Name"] ;
}
2. "Complicated Accessors" which "calculate" answers. For example, my
-parentname method gets the "name" key of the object pointed to by the
object's parent pointer value.
EXPECTED (AND NEEDED) RESULTS
It should use my accessors, both "simple" and "complicated".
Sorting by key "name" should work, using my accessor -name.
Sorting by key "parentname" should work, using -parentname.
Sorting by key "Name" should NOT work, since "Name" is a dictionary key, not
an accessor nor instance variable.
ACTUAL RESULTS
I get exactly the opposite in all 3 cases. The fact that sorting by key
"Name" works tells me that -sortedArrayUsingDescriptors is apparently using
NSDictionary -objectForKey instead of the valueForKey:pattern described
below.
Is there a way to get the results I expect and need? (Given: I'd rather not
use an NSArrayController.)
Jerry Krinock
(*) from ADC Home > Reference Library > Documentation > Cocoa > Data
Management > Key-Value Coding Programming Guide >
Default Search Pattern for valueForKey:
1. Search the receiverĀ¹s class for an accessor method whose name matches
the pattern -get<Key>, -<key>, or -is<Key>, in that order.
2. If no accessor is found, and the receiverĀ¹s class method
accessInstanceVariablesDirectly returns YES, the receiver is searched for an
instance variable whose name matches the pattern _<key>, _is<Key>, <key>, or
is<Key>, in that order.
3. If a matching accessor or instance variable is located the resulting
value is returned. If necessary, the value is first encapsulated as an
object as described in "Representing Data as Objects".
4. If no appropriate accessor or instance variable is found,
valueForUndefinedKey: is invoked for the receiver, and the value is
returned.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden