我正在尝试对 UserWrapper 对象数组进行排序。包装器对象包含对象 User,而 User 对象包含属性 UserName(我想作为排序依据)。
我们很容易对一组用户 (source) 进行排序,但是添加的 UserWrapper 层对我来说使事情变得复杂。请帮忙!
这是我的代码,适用于简单的用户数组:
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"UserName"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] ;
NSArray *descriptors = [NSArray arrayWithObject:nameDescriptor];
NSMutableArray *contactsStartingWithKey = [nameIndexesDictionary objectForKey:aKey];
[contactsStartingWithKey sortUsingDescriptors:descriptors]; // Exception thrown here because UserName is not a property of UserWrapper, but of UserWrapper.User
最佳答案
排序描述符的 key 参数也可以是key path,在你的例子中:
[[NSSortDescriptor alloc] initWithKey:@"User.UserName"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] ;
关于ios - 使用 NSSortDescriptor 在 iOS 中对包装对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23350502/