将 NSPredicate 与 Core Data 结合使用以建立深层关系
我有一个 NSArrayController,companiesController
绑定到顶级核心数据实体 Companies
。
一个公司
有很多部门
,一个部门
有很多员工
;这些由一对多关系、部门
和员工
表示。
基于 Employee
的属性 salary
我认为我可以动态地执行此操作,以便根据 UI 调用的方法内的薪水进行过滤:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY departments.employees.salary < %@", [NSNumber numberWithInt:23000]];
[companiesController setFilterPredicate:predicate];
唉,这给了我错误:<代码> - [NSCFSet比较:]:无法识别的选择器发送到实例。
I have an NSArrayController, companiesController
bound to a top level Core Data entity, Companies
.
A Company
has many Department
's, and a Department
has many Employee
; these are represented by the 1-to-many relationships, departments
and employees
.
Based on the attribute salary
of an Employee
I thought I could dynamically do this for filtering based on salary inside a UI-called method:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY departments.employees.salary < %@", [NSNumber numberWithInt:23000]];
[companiesController setFilterPredicate:predicate];
Alas, this gives me the error: -[NSCFSet compare:]: unrecognized selector sent to instance
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下不允许使用多个对多键。
相反,您可以执行以下操作:
代码更改(步骤 3):
以及新方法(步骤 2):
Multiple to-many keys are not allowed in this case.
Instead, you could do the following:
Code changes (step 3):
And the new method (step 2):
您也可以使用子查询来完成此操作。
获取所有部门。 “of”关系与公司与多个部门的关系相反:
或者,如果您有更多公司,并且只希望那些公司的员工工资“高于”某个金额。基于子查询结果的子查询
You could also do this using subqueries.
Get all departments. The 'of' relationship is the inverse of company to-many departments:
Or, if you have more companies and just want the companies that have an employee with a salary 'higher than' some amount. A subquery based on the result of a subquery