gpt4 book ai didi

objective-c - 使用sortedArrayUsingDescriptors排序时出错: "class is not key value coding-compliant for the key"

转载 作者:行者123 更新时间:2023-12-03 16:55:37 25 4
gpt4 key购买 nike

我有以下类的无序实例数组:

@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 个属性符合键值对。

发生什么事了?

最佳答案

区分大小写的问题。它在类中定义为 imageURL 并且您尝试按 imageUrl 进行排序。 :)

作为旁注,请确保遵循 Requirements of Collection Objects 。我不确定 NSURL 与排序的配合效果如何。

关于objective-c - 使用sortedArrayUsingDescriptors排序时出错: "class is not key value coding-compliant for the key",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com