Phi*_*ppS 8 sorting objective-c nssortdescriptor ios
在我的Xcode项目中,我有以下类:
地址
@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
Run Code Online (Sandbox Code Playgroud)
地点
@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
Run Code Online (Sandbox Code Playgroud)
在UITableViewController的子类中,有一个NSArray包含许多LDLocations的未排序对象.现在,我想根据LDAddress 的属性城市对NSArray的对象进行升序排序.
如何使用NSSortDescriptor对数组进行排序?我尝试了以下方法,但在排序数组时应用程序转储.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Address.city" ascending:YES];
[_locations sortedArrayUsingDescriptors:@[sortDescriptor]];
Run Code Online (Sandbox Code Playgroud)
kub*_*ubi 14
尝试将第一个键设为小写.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"address.city" ascending:YES];
Run Code Online (Sandbox Code Playgroud)
Ada*_*dam 14
您还可以使用块对数组进行排序:
NSArray *sortedLocations = [_locations sortedArrayUsingComparator: ^(LDAddress *a1, LDAddress *a2) {
return [a1.city compare:a2.city];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14131 次 |
| 最近记录: |