I have an NSArray of Objects of type NSManagedObject that are returned from a fetch result I perform in Core Data. The NSArray contains objects, as I am able to verify this after the query by printing the contents of the NSArray to the console. My problem however is that I am unable to do fast enumeration over this array using objects of the type of entity that I have retrieved from the query. The exact error I am getting at runtime is the following:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[My_Entity_Name countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa9f0090'
My for loop that I am using doesn't even get to run because it breaks at the for loop condition:
for (MyEntityType *entityType in self.entityArray) {
...
}
The actual fetch command I use to populate the array self.entityArray is:
self.entityArray = [[Singleton sharedInstance] retrieveEntities:self.mainEntity.relationshipEntity.relationshipEntityId];
in turn, this is how my retrieveEntity method looks like:
- (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;
}
}
Why am I getting the above error?