views:

103

answers:

2

I have a view controller that can fetch many different types of entities from my MOC. How can I tell what the entity is for an object of the type NSManagedObject?

+1  A: 

By calling the entity method.

Martin Cote
NSManagedObject *object = (NSManagedObject *)[locations objectAtIndex:indexPath.row]; NSEntityDescription *entityDescr = [object entity]; NSLog(@"%@", [entityDescr name]);This throws an error... *** -[NSKnownKeysDictionary1 entity]: unrecognized selector sent to instance 0x3e39cc0
E-Madd
+2  A: 

[managedObject entity] will give you an NSEntityDescription. From there you can send it -name to get the entity's name that's a bit more user-friendly.

Giao
NSManagedObject *object = (NSManagedObject *)[locations objectAtIndex:indexPath.row];NSEntityDescription *entityDescr = [object entity];NSLog(@"%@", [entityDescr name]);throws and error... *** -[NSKnownKeysDictionary1 entity]: unrecognized selector sent to instance 0x3e39cc0
E-Madd
That's because the returned value from `[locations objectAtIndex:indexPath.row]` isn't an NSManagedObject. I bet if you NSLog that object, it won't show up as an NSManagedObject.
Giao
Hey, you're right. It's showing as an NSDictionary. Hmmmm. Thanks.
E-Madd