从 NSSet 中检索 NSManagedObject

发布于 2024-10-17 15:27:59 字数 202 浏览 5 评论 0原文

我有两个实体,它们之间具有一对多关系。持有“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-10-24 15:27:59

你有几个选择。

NSArray* objectsArray = [yourSet allObjects];

这将使用集合中的所有对象填充objectsArray,此时您可以枚举它们以查找所需的一个或多个对象。

您还可以使用如下谓词:(

NSPredicate *desiredWeekPredicate = [NSPredicate predicateWithFormat:@"currentWeek == %d", currentWeekYouWant];
NSSet *objectsWithDesiredWeek = [yourSet filteredSetUsingPredicate:predicate];

您的谓词看起来会有所不同,具体取决于您存储 currentWeek 的方式)。如果每个 currentWeek 只有一个对象,则只需对objectsWithDesiredWeek 设置调用 -anyObject 即可获取对象。如果您可以拥有多个具有相同 currentWeek 的对象,那么在objectsWithDesiredWeek 上调用 -allObjects 方法将为您提供一个数组,其中包含使用所需周的所有对象。

You have a couple options.

NSArray* objectsArray = [yourSet allObjects];

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:

NSPredicate *desiredWeekPredicate = [NSPredicate predicateWithFormat:@"currentWeek == %d", currentWeekYouWant];
NSSet *objectsWithDesiredWeek = [yourSet filteredSetUsingPredicate:predicate];

(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文