2011-04-05 47 views
1

我有下面的类的实例的无序排列:错误而与sortedArrayUsingDescriptors排序:“类不是密钥值编码兼容的关键”

@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 

我试图用对它进行排序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]]; 

如果我尝试使用ImageUrl属性进行排序,我得到一个相当奇怪的错误:

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

该属性与其他两个属性合成,所以我期望3是键值兼容。

发生了什么事?

回答

4

区分大小写的问题。它在课程中定义为imageURL,您正在尝试按imageUrl进行排序。 :)

作为便笺请确保你按照Requirements of Collection Objects。我不确定NSURL在排序方面的效果如何。

相关问题