iphone 子对象的谓词

发布于 2024-11-04 21:24:06 字数 397 浏览 2 评论 0原文

我在谓词编程指南中看到这一点:

如果使用一对多关系,谓词的构造会略有不同。例如,如果要获取其中至少有一个员工的名字为“Matthew”的部门,则可以使用 ANY 运算符,如下例所示:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"ANY employees.firstName like 'Matthew'"];

通过上述构造,它将返回包含所有员工的部门与其关联的对象,前提是至少有一名员工的名字为“Matthew”。

我想修改它以仅获取名字为“Matthew”的员工。 因此,如果该部门只有 2 名员工与该名字匹配,则只应返回 2 个员工对象。

我怎样才能实现这个目标? 谢谢。

I see this in the predicate programming guide:

If you use a to-many relationship, the construction of a predicate is slightly different. If you want to fetch Departments in which at least one of the employees has the first name "Matthew," for instance, you use an ANY operator as shown in the following example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"ANY employees.firstName like 'Matthew'"];

With the above construction, it would return the department with all employees objects associated to it, provided at least one of the employees have first name as 'Matthew'.

I would like to modify this to fetch ONLY those employees with first name as 'Matthew'.
So if the department has only 2 employees matching this first name, only 2 employee objects should be returned.

How can I achieve this?
thanks.

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

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

发布评论

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

评论(1

夏花。依旧 2024-11-11 21:24:06

您正在对一组 Department 对象执行此操作,因此您只能取回 Department 对象。如果您想要 Employee 对象,则必须执行以下操作:

Department *aDepartment = ...;
NSArray *employees = [aDepartment employees];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"firstName = 'Matthew'"];
NSArray *matthews = [employees filteredArrayUsingPredicate:filter];

此外,由于您的 LIKE 过滤器没有任何 ?或者其中包含 * 符号,相当于“isEqual:”比较。

You're executing this agains a set of Department objects, so you can only get back Department objects. If you want Employee objects instead, you'd have to do something like this:

Department *aDepartment = ...;
NSArray *employees = [aDepartment employees];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"firstName = 'Matthew'"];
NSArray *matthews = [employees filteredArrayUsingPredicate:filter];

Also, since your LIKE filter doesn't have any ? or * symbols in it, it is equivalent to an "isEqual:" comparison.

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