在我的 Xcode 项目中,我有以下类:
Address
@interface LDAddress : NSObject{
NSString *street;
NSString *zip;
NSString *city;
float latitude;
float longitude;
}
@property (nonatomic, retain) NSString *street;
@property (nonatomic, retain) NSString *zip;
@property (nonatomic, retain) NSString *city;
@property (readwrite, assign, nonatomic) float latitude;
@property (readwrite, assign, nonatomic) float longitude;
@end
Location
@interface LDLocation : NSObject{
int locationId;
NSString *name;
LDAddress *address;
}
@property (readwrite, assign, nonatomic) int locationId;
@property (nonatomic, retain) LDAddress *address;
@property (nonatomic, retain) NSString *name;
@end
在 UITableViewController 的子类中,有一个 NSArray,其中包含许多未排序的 LDLocations 对象。现在,我想根据属性对 NSArray 的对象进行升序排序cityLD地址的。
如何使用 NSSortDescriptor 对数组进行排序?
我尝试了以下操作,但应用程序在对数组进行排序时转储。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];