在我的Xcode項目,我有以下類別:排序NSArray的自定義對象
地址
@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
位置
@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的子類,有一個包含大量LDLocations未排序對象的NSArray。現在,我想根據LDAddress的城市對NSArray的對象進行升序排序。
如何使用NSSortDescriptor對數組進行排序? 我嘗試了以下操作,但應用程序在對數組進行排序時轉儲。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];
你試過只是@ 「城市」 爲重點? –