用sortedArrayUsingDescriptors和Key Paths排序 [英] Sorting with sortedArrayUsingDescriptors and Key Paths
问题描述
我有一个无序的数组,包含以下类的实例:
@interface地方: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按国家和城市。 NSSortDescriptor的文档说initWithKey的arg是一个Key Path。
但是,如果我尝试使用NSortDescriptor,比如:
NSSortDescriptor * country = [[[NSSortDescriptor alloc] initWithKey:@country.cityascending:YES] autorelease];
我收到错误:
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: .google.com]]];
NSSortDescriptor * sort = [[[NSSortDescriptor alloc] initWithKey:@country.cityascending:YES] autorelease];
NSArray * sorted = [bag sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort,nil]];
* 由于未捕获异常而终止应用程序'NSUnknownKeyException',
reason:'[< NSCFString 0x6ae8>
valueForUndefinedKey:]:这个类是
不是关键值编码符合
关键城市' p>
我可以使用键路径吗?
解决方案您可能希望使用:
NSSortDescriptor * country = [[[NSSortDescriptor alloc] initWithKey:@countryascending:YES] autorelease];
NSSortDescriptor * city = [[[NSSortDescriptor alloc] initWithKey:@cityascending:YES] autorelease];
NSArray * sorted = [bag sortedArrayUsingDescriptors:[NSArray arrayWithObjects:country,city,nil]];
传递到
的数组sortedArrayUsingDescriptors:
排序关键优先级。排序描述符提供要比较的关键路径(以及所需的顺序:升序/降序)。
country.city的排序描述符将询问每个地方的国家然后作为返回的国家(这是一个NSString)为其城市。我们都知道NSString不是价值编码符合关键城市的;)
你需要一个
国家的排序描述符。城市
如果您的放置
实例具有属性country
code> city 。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?
解决方案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 yourPlace
instances had a propertycountry
, which themselves had a propertycity
.这篇关于用sortedArrayUsingDescriptors和Key Paths排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!