0

我有一个 NSArray 类型的 NSManagedObject 对象,这些对象是从我在 Core Data 中执行的获取结果返回的。NSArray 包含对象,因为我可以在查询后通过将 NSArray 的内容打印到控制台来验证这一点。然而,我的问题是我无法使用从查询中检索到的实体类型的对象对该数组进行快速枚举。我在运行时遇到的确切错误如下:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[My_Entity_Name countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa9f0090'

我正在使用的 for 循环甚至无法运行,因为它在 for 循环条件下中断:

for (MyEntityType *entityType in self.entityArray) {
...
}

我用来填充数组 self.entityArray 的实际 fetch 命令是:

self.entityArray = [[Singleton sharedInstance] retrieveEntities:self.mainEntity.relationshipEntity.relationshipEntityId];

反过来,这就是我的 retrieveEntity 方法的样子:

- (NSArray *)retrieveEntities:(NSNumber *)relationshipEntityAttributeId {

    NSManagedObjectContext *context = [[DataEngine sharedInstance] managedObjectContext];
    NSError *error;

    // Create fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:ENTITY_NAME inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    // Create predicate
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"relationshipEntity.relationshipAttributeId == %@", relationshipEntityAttributeId];
    [fetchRequest setPredicate:pred];

    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
    if ([items count]>0) {
        return items[0];
    } else {
        return nil;
    }
}

为什么我会收到上述错误?

4

2 回答 2

0
于 2013-11-24T06:03:51.127 回答
0

代码中有一个错字,你填充 self.entityAray,然后从 self.entityArray 中绘制。这是您的示例中的一个小错字,或者它解释了您的错误。在查找 self.entityArray 时,由于拼写错误而无法找到它 en 抛出 NSInalidArgumentException

于 2013-11-24T07:50:07.283 回答