我创建了一堆Group Entities
:
for (NSString *groupID in result) {
group = [Group MR_createInContext:context];
group.groupID = [NSNumber numberWithInteger:[groupID integerValue]];
}
然后,我希望按类别列出它们:
NSArray *groups = [Group MR_findAllSortedBy:@"groupID" ascending:TRUE inContext:context];
for (Group *group in groups) {
DLog(@"group.groupID: %@", group.groupID);
DLog(@"group: %@", group);
}
产生错误:
-[__NSCFNumber caseInsensitiveCompare:]: unrecognized selector sent to instance 0x2595d2c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber caseInsensitiveCompare:]: unrecognized selector sent to instance 0x2595d2c0'
我的论坛Entity
是自动生成的:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Group : NSManagedObject
@property (nonatomic, retain) NSNumber * groupID;
@end
@interface Group (CoreDataGeneratedAccessors)
@end
如果我使用魔法记录进行同样的提取,那就很好了:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Group"];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"groupID" ascending:TRUE selector:nil];
NSArray *sorters = [NSArray arrayWithObject:sort];
[fetchRequest setSortDescriptors:sorters];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
为什么我收到错误的任何想法?
答案 0 :(得分:2)
所以我检查了github(https://github.com/magicalpanda/MagicalRecord)上的魔法记录回购,我没有看到你正在使用的方法(可能是因为文档中有关于排序方法的bug
获取已排序的实体但我看到了另外两种建议用于获取已排序实体的方法。
[entity MR_findAllSortedByProperty:@"property" ascending:YES] // single property
[entity MR_findAllSortedByProperty:@"oneProperty,secondProp" ascending:YES] // mutliple properties