1
votes

I have an unordered array of 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

I'm trying to sort it using sortedArrayUsingDescriptors :

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 *country = [[[NSSortDescriptor alloc] initWithKey:@"country" 
                                                         ascending:YES]autorelease];

NSSortDescriptor *city = [[[NSSortDescriptor alloc] initWithKey:@"city"
                                                      ascending:YES] autorelease];
// this is were everything goes wrong 
NSSortDescriptor *url = [[[NSSortDescriptor alloc] initWithKey:@"imageUrl" 
                                                     ascending:YES] autorelease]; 

NSArray *sorted = [bag sortedArrayUsingDescriptors:[NSArray arrayWithObjects: country, city, nil]];

If I try to use the imageUrl property to sort, I get a rather strange error:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key imageUrl.'

That property is synthesized just like the other two, so I would expect the 3 to be key-value compliant.

What's going on?

1

1 Answers

4
votes

Case sensitive issue. It is defined in the class as imageURL and you are trying to sort by imageUrl. :)

As a side note make sure you follow the Requirements of Collection Objects. I am not sure how well NSURL works with sorting.