NSSortDescriptor - 基于另一个数组排序描述符 [英] NSSortDescriptor - Sort descriptor based on another array
问题描述
我有一个核心数据应用程序。我想要获取一种对象, User
。 用户
有属性 userId
。
我有另一个数组 userId
s, [1,4,3,5 ]
。我想创建一个 NSSortDescriptor
,它根据用户
对象> userId s。
这是否可能,我该怎么办?
更新
-
我为User对象添加了一个transformable属性,用于存储用户id数组。
-
我尝试了以下排序描述符:
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@userIdascending:YES comparator:^ NSComparisonResult(id obj1,id obj2){
NSUInteger idx1 = [self.user.followingIds indexOfObject:[obj1 valueForKey:@userId ]];
NSUInteger idx2 = [self.user.followingIds indexOfObject:[obj2 valueForKey:@userId]];
return idx1 - idx2;
}];
严重的应用程序错误。在核心数据更改处理期间捕获异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察器中的错误。 [< __ NSCFNumber 0xa337400> valueForUndefinedKey:]:此类不是关键值userId的键值编码兼容。 with userInfo {
NSTargetObjectUserInfoKey = 2502;
NSUnknownUserInfoKey = userId;
}
***由于未捕获异常NSUnknownKeyException而终止应用程序,原因:'[< __ NSCFNumber 0xa337400> valueForUndefinedKey:]:此类不是键值编码符合关键userId。'
Update 2 用户对象。任何想法如何关系应该看起来?见附图。那是对的吗?
>
这不能使用排序描述符,您必须在获取结果后应用自定义比较函数:
NSArray * userIds = ...; //例如@ [@ 1,@ 4,@ 3,@ 5]
NSArray * results = ...; //获取请求的结果
NSArray * sorted = [results sortedArrayUsingComparator:^ NSComparisonResult(id obj1,id obj2){
NSUInteger idx1 = [userIds indexOfObject:[obj1 valueForKey:@userId]];
NSUInteger idx2 = [userIds indexOfObject:[obj2 valueForKey:@userId]];
return idx1 - idx2;
}];
I hava a core data application. And I want to fetch one kind of object, User
. User
have the property userId
.
I have another array with userId
s, [1, 4, 3, 5]
. And I would like to create a NSSortDescriptor
that sorts my User
objects based on the order of the userId
s in the array.
Is that possible, and how should I do that?
Update
I have now tried the following.
I added a transformable property to my User object, where I store the user ids array.
I have tried the following sort descriptor:
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"userId" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) { NSUInteger idx1 = [self.user.followingIds indexOfObject:[obj1 valueForKey:@"userId"]]; NSUInteger idx2 = [self.user.followingIds indexOfObject:[obj2 valueForKey:@"userId"]]; return idx1 - idx2; }];
And I'm getting the following error:
Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. [<__NSCFNumber 0xa337400> valueForUndefinedKey:]: this class is not key value coding-compliant for the key userId. with userInfo {
NSTargetObjectUserInfoKey = 2502;
NSUnknownUserInfoKey = userId;
}
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFNumber 0xa337400> valueForUndefinedKey:]: this class is not key value coding- compliant for the key userId.'
Update 2
Would also like to add a relationship Followers
between User
objects. Any ideas how that relationship should look? See attached image. Is that correct?
This can not be done with a sort descriptor, you have to apply a custom comparator function after fetching the results:
NSArray *userIds = ...; // e.g. @[@1, @4, @3, @5]
NSArray *results = ...; // result of fetch request
NSArray *sorted = [results sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSUInteger idx1 = [userIds indexOfObject:[obj1 valueForKey:@"userId"]];
NSUInteger idx2 = [userIds indexOfObject:[obj2 valueForKey:@"userId"]];
return idx1 - idx2;
}];
这篇关于NSSortDescriptor - 基于另一个数组排序描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!