从 NSSet 中检索 NSManagedObject
我有两个实体,它们之间具有一对多关系。持有“many”的实体具有预期的 NSSet 属性。我不确定如何访问 NSSet 中的特定元素。 NSSet 包含具有多个属性的对象,其中之一是 currentWeek。我想访问 NSSet 中具有特定 currentWeek 的对象。
我知道我可以执行 FetchRequest 来找到它,但我认为有一种更直接的方法使用 NSSet。
I've got two entities with a one-to-many relationship between them. The entity that holds "many" has the expected NSSet property. What I'm not sure is how to access a specific element in the NSSet. The NSSet contains objects that have several properties, one of which is currentWeek. I want to access an object within my NSSet that has a specific currentWeek.
I know I can do a FetchRequest to find it, but I assume there is a more straightforward way using the NSSet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几个选择。
这将使用集合中的所有对象填充objectsArray,此时您可以枚举它们以查找所需的一个或多个对象。
您还可以使用如下谓词:(
您的谓词看起来会有所不同,具体取决于您存储 currentWeek 的方式)。如果每个 currentWeek 只有一个对象,则只需对objectsWithDesiredWeek 设置调用 -anyObject 即可获取对象。如果您可以拥有多个具有相同 currentWeek 的对象,那么在objectsWithDesiredWeek 上调用 -allObjects 方法将为您提供一个数组,其中包含使用所需周的所有对象。
You have a couple options.
This will populate objectsArray with all the objects in the set, at which point you can enumerate through them looking for the object or objects you want.
You could also use a predicate something like this:
(Your predicate will look different depending on how you store currentWeek). If you only have one object per currentWeek, you can just call -anyObject on the objectsWithDesiredWeek set to get your object. If you can have more than one object with the same currentWeek then the calling the -allObjects method on objectsWithDesiredWeek will get you an array with all the objects that use your desired week.