如何从 NSEntityDescription 中获取实体
我有一个 NSTreeController (treeController) 和一个 CoreData 数据库。我想要 NSTreeController 所选对象背后的实体。
我使用它来获取正确的 NSEntityDescription:
[[[[treeController selectedObjects] objectAtIndex:0] representedObject] entity]
现在我有了我的 testEntity,并且我想分配我通过上面的代码获得的 NSEntityDescription 描述的实体对象。
MyEntityClass *testEntity;
testEntity = ???
我找不到方法。还有另一种方法可以获取 NSTreeController 所选对象后面的实体对象吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,当您使用
NSTreeController
管理NSOutlineView
时,NSTreeController
的-selectedObjects
方法应该返回一个NSManagedObject
实例数组。您可以使用
NSManagedObject
的-entity
方法查询这些实例的实体。但是,您被称为
-representedObject
(不是NSManagedObject
方法)这一事实意味着您的树控制器正在处理某种其他类型的对象。它是什么?你的树控制器是如何配置的?如果记录[[treeController selectedObjects] objectAtIndex:0]
的输出,您会得到什么?Usually when you're using an
NSTreeController
to manage anNSOutlineView
, the‑selectedObjects
method ofNSTreeController
should return an array ofNSManagedObject
instances.You can query those instances for their entities using the
-entity
method ofNSManagedObject
.However, the fact that you're called
-representedObject
(not anNSManagedObject
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]
?