简体   繁体   English

使用sortedArrayUsingDescriptors排序时出错:“类不是密钥值编码兼容的密钥”

[英]Error while sorting with sortedArrayUsingDescriptors : “class is not key value coding-compliant for the key”

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 : 我正在尝试使用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: 如果我尝试使用imageUrl属性进行排序,我会得到一个相当奇怪的错误:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key imageUrl.' *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:此类不是键值imageUrl的键值编码兼容。

That property is synthesized just like the other two, so I would expect the 3 to be key-value compliant. 该属性与其他两个属性合成,因此我希望3是符合键值的。

What's going on? 这是怎么回事?

Case sensitive issue. 区分大小写问题。 It is defined in the class as imageURL and you are trying to sort by imageUrl. 它在类中定义为imageURL ,您尝试按imageUrl排序。 :) :)

As a side note make sure you follow the Requirements of Collection Objects . 作为旁注,请确保遵循收集对象要求 I am not sure how well NSURL works with sorting. 我不确定NSURL在排序方面的效果如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM