Search code examples
cocoacore-dataentitynsentitydescription

How to get an entity out of a NSEntityDescription


I have a NSTreeController (treeController) and a CoreData database. I want the entity behind the selected object of my NSTreeController.

I use this to get the correct NSEntityDescription:

[[[[treeController selectedObjects] objectAtIndex:0] representedObject] entity]

Now I have my testEntity and I want to assign the entity object described by the NSEntityDescription I get with the code above.

MyEntityClass *testEntity;
testEntity = ???

I can't find a method. Is there another way to get the entityObject behind the selected object of the NSTreeController?

Thx!


Solution

  • Usually when you're using an NSTreeController to manage an NSOutlineView, the ‑selectedObjects method of NSTreeController should return an array of NSManagedObject instances.

    You can query those instances for their entities using the -entity method of NSManagedObject.

    However, the fact that you're called -representedObject (not an NSManagedObject method) means that your tree controller is handling some other sort of object. What is it? How is your tree controller configured? What do you get if you log the output of [[treeController selectedObjects] objectAtIndex:0]?