Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting with sortedArrayUsingDescriptors and Key Paths

I have an unordered array with instances of the following class:

@interface Place : NSObject {

}

@property (nonatomic, copy) NSString *country;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, retain) NSURL   *imageURL;


-(id) initWithCountry: (NSString *) aCountry city: (NSString *) aCity imageUrl: (NSURL *) aUrl;


@end

and I'm trying to sort it with sortedArrayUsingDescriptors by country and city. The docs for NSSortDescriptor say that the arg of initWithKey: is a Key Path.

However, If I try to use a NSortDescriptor such as:

NSSortDescriptor *country = [[[NSSortDescriptor alloc] initWithKey:@"country.city" ascending:YES]autorelease];

I get an error:

NSMutableSet *bag = [[[NSMutableSet alloc] init] autorelease];
[bag addObject:[[Place alloc] initWithCountry:@"USA" 
                                         city:@"Springfield" 
                                     imageUrl:[NSURL URLWithString:@"http://www.agbo.biz"]]];
[bag addObject:[[Place alloc] initWithCountry:@"Afghanistan" 
                                         city:@"Tora Bora" 
                                     imageUrl:[NSURL URLWithString:@"http://www.agbo.biz"]]];
[bag addObject:[[Place alloc] initWithCountry:@"USA" 
                                         city:@"Chicago" 
                                     imageUrl:[NSURL URLWithString:@"http://www.agbo.biz"]]];

[bag addObject:[[Place alloc] initWithCountry:@"USA" 
                                         city:@"Chicago" 
                                     imageUrl:[NSURL URLWithString:@"http://www.google.com"]]];


NSSortDescriptor *sort = [[[NSSortDescriptor alloc] initWithKey:@"country.city" ascending:YES]autorelease];
NSArray *sorted = [bag sortedArrayUsingDescriptors:[NSArray arrayWithObjects: sort, nil]];

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCFString 0x6ae8> valueForUndefinedKey:]: this class is not key value coding-compliant for the key city.'

Can I use a key path or not? What am I doing wrong?

like image 476
cfischer Avatar asked Apr 05 '11 20:04

cfischer


1 Answers

You may want to use this:

NSSortDescriptor *country = [[[NSSortDescriptor alloc] initWithKey:@"country" ascending:YES]autorelease];
NSSortDescriptor *city = [[[NSSortDescriptor alloc] initWithKey:@"city" ascending:YES]autorelease];
NSArray *sorted = [bag sortedArrayUsingDescriptors:[NSArray arrayWithObjects: country, city, nil]];

The array that's passed to sortedArrayUsingDescriptors: defines the sorting key priorities. The sort descriptors provide the keypaths to compare (and the wanted order: ascending/descending).

A sort descriptor of "country.city" will ask each place for its country and then as the returned country (which is an NSString) for its city. And we all know that NSString is not value coding-compliant for the key city ;)

You'd need a sort descriptor of "country.city" if your Place instances had a property country, which themselves had a property city.

like image 200
Regexident Avatar answered Sep 19 '22 18:09

Regexident