• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSSortDescriptor not working with array of custom classes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSSortDescriptor not working with array of custom classes


  • Subject: Re: NSSortDescriptor not working with array of custom classes
  • From: Ken Tozier <email@hidden>
  • Date: Sun, 4 Nov 2007 17:50:18 -0500


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



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSSortDescriptor not working with array of custom classes
      • From: Shawn Erickson <email@hidden>
  • Prev by Date: Re: Locating managed objects within ObjectAlloc (was Re: Garbage collection, core data, and tight loops)
  • Next by Date: Re: I'm losing my memory (GC & NSImageView)
  • Previous by thread: Re: DRY properties - setting default @synthesize instead of @dynamic
  • Next by thread: Re: NSSortDescriptor not working with array of custom classes
  • Index(es):
    • Date
    • Thread