【发布时间】:2013-05-07 04:56:27
【问题描述】:
我有一个核心数据应用程序。我想获取一种对象User。 User 拥有userId 的属性。
我有另一个数组,userIds,[1, 4, 3, 5]。我想创建一个NSSortDescriptor,它根据数组中userIds 的顺序对我的Userobjects 进行排序。
这可能吗,我应该怎么做?
更新
我现在尝试了以下方法。
我向我的 User 对象添加了一个可转换的属性,我在其中存储了用户 ids 数组。
-
我尝试了以下排序描述符:
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; }];
我收到以下错误:
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.'
更新 2
还想在User 对象之间添加关系Followers。任何想法应该如何看待这种关系?见附图。那是对的吗?
【问题讨论】:
-
您是否将 userId 存储在您的数据库中。如果这样,请使用 amit answer 来完成它
标签: ios objective-c cocoa-touch core-data nssortdescriptor