NSSortDescriptor - 基于另一个数组排序描述符 [英] NSSortDescriptor - Sort descriptor based on another array

查看:124
本文介绍了NSSortDescriptor - 基于另一个数组排序描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据应用程序。我想要获取一种对象, User 用户有属性 userId



我有另一个数组 userId s, [1,4,3,5 ] 。我想创建一个 NSSortDescriptor ,它根据用户对象> userId s。



这是否可能,我该怎么办?



更新




  1. 我为User对象添加了一个transformable属性,用于存储用户id数组。


  2. 我尝试了以下排序描述符:

      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 userIds, [1, 4, 3, 5]. And I would like to create a NSSortDescriptor that sorts my Userobjects based on the order of the userIds in the array.

Is that possible, and how should I do that?

Update

I have now tried the following.

  1. I added a transformable property to my User object, where I store the user ids array.

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆