NSPredicate 检索具有特定孩子的所有父母? (IOS)

发布于 12-11 09:18 字数 679 浏览 1 评论 0原文

假设我在 IOS 中有以下核心数据结构:

Parent1
    Child1.1: Text='A', Number=1
    Child1.2: Text='B', Number=1
Parent2
    Child2.1: Text='B', Number=1
    Child2.2: Text='A', Number=2
Parent3
    Child3.1: Text='B', Number=2
    Child3.2: Text='A', Number=1

我想检索所有拥有 Text='A' 和 Number=1 的孩子的父母(上例中的 Parent1 和 Parent3)。

基本上我需要的是父实体的谓词,如下所示:

ANY (child.Text = 'A' AND child.Number = 1)

但这不起作用 - 似乎我不能在 ANY 关键字后面添加括号。

以下也不好(即使它是有效的):

ANY child.Text = 'A' AND ANY child.Number = 1

因为它也返回 Parent2 。

有没有办法在谓词中执行此操作,或者我是否必须以编程方式执行此操作(例如,仅检索子级而不使用 ANY 关键字并从后向关系构造父级数组)?

Say I have the following Core Data structure in IOS:

Parent1
    Child1.1: Text='A', Number=1
    Child1.2: Text='B', Number=1
Parent2
    Child2.1: Text='B', Number=1
    Child2.2: Text='A', Number=2
Parent3
    Child3.1: Text='B', Number=2
    Child3.2: Text='A', Number=1

I want to retrive all parents that have a child with Text='A' and Number=1 (Parent1 and Parent3 in the above example).

Basically what I need is a predicate on the Parent entity that looks like:

ANY (child.Text = 'A' AND child.Number = 1)

But this doesn't work - it seems that I cannot have parentheses following an ANY keyword.

The following isn't good either (even though it's valid):

ANY child.Text = 'A' AND ANY child.Number = 1

because it returns Parent2 as well.

Is there a way to do this in a predicate, or do I have to do it programmatically (e.g. retrieve just the children without using an ANY keyword and construct an array of parents from the back relationships)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

美人如玉2024-12-18 09:18:07

在谓词中,您必须使用 == 来表示相等 - 那么它有效吗?

您还可以查看 NSCompoundPredicate,用于将谓词部分与 AND 和 OR 组合,例如


NSMutableArray* predicatesToCombine = [[NSMutableArray alloc]init];

[predicatesToCombine addObject:@"child.Text == 'A'"];
[predicatesToCombine addObject:@"child.Number == 1"];

NSPredicate* predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicatesToCombine];

...

[predicatesToCombine release];

In a predicate you have to use == for equality - does it work then?

You can also have a look at the NSCompoundPredicate for combining predicate parts with AND and OR, like


NSMutableArray* predicatesToCombine = [[NSMutableArray alloc]init];

[predicatesToCombine addObject:@"child.Text == 'A'"];
[predicatesToCombine addObject:@"child.Number == 1"];

NSPredicate* predicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicatesToCombine];

...

[predicatesToCombine release];

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