Skip navigation.
 
mlRe: NSSortDescriptor not working with array of custom classes
FROM : Ken Tozier
DATE : Sun Nov 04 23:50:18 2007

> On Oct 19, 2007, at 2:26 PM, Jim Correia wrote:
>

>> Have you been able to isolate the behavior down to a standalone 
>> sample?
>>
>> I find that to be useful because it shows me my bug, or gives me 
>> something I can dump directly into radar. If you do that, send me 
>> the sample too, because I'm curious what is going on here :-)
>>


Well I ran into this problem again with another class and after 
several hours of head scratching,  it appears that when initializing 
sort descriptors you *must* use the actual name of any property you 
want to sort on. You can't use accessors to those properties.

For example: I have a custom page view class whose datasource is a 
reference to a page model class and the model class stores page data 
in an internal NSMutableDictionary. In testing, all the accessor 
methods worked but when used to initialize a sort descriptor, they 
all return null

// next line shows that "pages" is indeed an NSArray
NSLog(@"pages class: %@", [pages class]);

// next line shows that the first page is a valid "PMPageView" instance
NSLog(@"first page: %@", [pages objectAtIndex: 0]);

// next 4 lines all fetch the page number correctly
NSLog(@"direct call page view accessor: %i", [[pages objectAtIndex: 
0] pageNumber]);
NSLog(@"direct call model accessor: %@", [[[pages objectAtIndex: 0] 
model] pageNumber]);
NSLog(@"direct call model raw accessor: %@", [[[[pages objectAtIndex: 
0] model] valueForKeyPath: @"pageRecord"] objectForKey: 
@"page_number"]);
NSLog(@"direct call model raw accessor 2: %@", [[[pages 
objectAtIndex: 0] model] valueForKeyPath: @"pageRecord.page_number"]);
   
// these all return null
NSLog(@"page view accessor: %@", [[pages objectAtIndex: 0] 
valueForKeyPath: @"pageNumber"]);
NSLog(@"model accessor: %@", [[pages objectAtIndex: 0] 
valueForKeyPath: @"model.pageNumber"]);
NSLog(@"model raw accessor: %@", [[pages objectAtIndex: 0] 
valueForKeyPath: @"model.pageRecord.page_number"]);

// all of the following sort descriptors fail
[desc addObject: [[NSSortDescriptor alloc] initWithKey: @"pageNumber" 
ascending: YES]];
[desc addObject: [[NSSortDescriptor alloc] initWithKey: 
@"model.pageNumber" ascending: YES]];
[desc addObject: [[NSSortDescriptor alloc] initWithKey: 
@"model.pageRecord.page_number" ascending: YES]];
   

// next line fails to sort the page array
NSArray                *sortedPages        = [pages sortedArrayUsingDescriptors: desc];

// If I define a valueForKeyPath function for the "PMPageView" class 
like so
- (id) valueForKeyPath:(id) inKey
{
   if ([inKey isEqualToString: @"model.pageNumber"])
       return [model pageNumber];
   else
       return [super valueForKeyPath: inKey];
}

// the following works, but having to write such an ugly hacked 
"valueForKeyPath" method shouldn't be necessary
[desc addObject: [[NSSortDescriptor alloc] initWithKey: 
@"model.pageNumber" ascending: YES]];
NSArray                *sortedPages        = [pages sortedArrayUsingDescriptors: desc];

I must be confused about how to correctly define sort descriptors or 
perhaps KVC, but I would think that using accessor names in a key 
path should work. Is that not the case?

Any help appreciated.

Ken

Related mailsAuthorDate
mlRe: NSSortDescriptor not working with array of custom classes Ken Tozier Nov 4, 23:50
mlRe: NSSortDescriptor not working with array of custom classes Shawn Erickson Nov 5, 00:05
mlRe: NSSortDescriptor not working with array of custom classes Ken Tozier Nov 5, 00:25
mlRe: NSSortDescriptor not working with array of custom classes Shawn Erickson Nov 5, 02:07