Cocoa 绑定:绑定到“许多”对象一对多关系的结束
使用员工部门示例,我想要做的是将列绑定到“[电子邮件受保护]”如下所述:
实体员工
- 属性:“firstName”、“lastName”、“hoursWorked”
- 关系:“部门”
实体部门
- 属性:“name”
- 关系:“employees”
我想要一个表格来显示一些信息有关部门的摘要信息。
我将第一列绑定到“Departments”数组控制器“arrangedObjects.name”。 我可以通过绑定到“arrangedObjects.employees.@count”来有一列显示部门中的员工人数,
但是我无法获得员工工作的小时数总和,因为我假设我可以通过绑定到“[电子邮件受保护]”
错误get 的内容类似于“[<_NSFaultingMutableSet 0x1acea0> ; addObserver:forKeyPath:options:context:] 不支持 Key path: @sum.hoursWorked"
我相信这是因为无法绑定到一对多关系的多端。如果是的话我该如何做我想做的事?
为了获得额外的积分,假设每个员工还有另一个属性“种族”,我还希望我的摘要表显示每个部门中独特种族的数量。
提前致谢。
Using the Employees-Departments example what I want to do is bind a column to "[email protected]" as outlined below:
Entity Employee
- attributes: "firstName", "lastName", "hoursWorked"
- relationship: "departments"
Entity Department
- attributes: "name"
- relationship: "employees"
I want a table which will display some summary info about departments.
I bind the first column to my "Departments" array controller, "arrangedObjects.name".
I can have a column displaying the number of employees in a department by binding to "arrangedObjects.employees.@count"
However I can't get a sum of the hoursWorked by employees as I assume I might by binding to "[email protected]"
The error I get is along the lines of "[<_NSFaultingMutableSet 0x1acea0> addObserver:forKeyPath:options:context:] is not supported. Key path: @sum.hoursWorked"
I believe this is because it is not possible to bind to the many end of a to-many relationship. If so how can I do what I want to do?
For extra credit, say each employee also has another attribute, "race", I would also like my summaries table to show the number of unique races in each department.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了和你一样的错误。看来,虽然您可以通过执行以下操作来获取员工集并对其执行一些聚合操作:
Department* dept = ;
NSSet* 员工 = dept.employees;
NSNumber* sumOfHoursWorked = [employees valueForKeyPath: @"@sum.hoursWorked"];
绑定的时候是有区别的。绑定要求观察关键路径,而不是评估一次。鉴于此,为什么不能绑定到这些关键路径就有点道理了。有点。有点。
现在。至于解决方案,在这种情况下我通常会编写一个很小的 NSValueTransformer 子类来完成我需要的操作,然后将其插入 IB 中。这样,我编写了所需的十行代码,但最终不会因为需要简单的聚合而完成整个 NSTableView 数据源的说明。在这种情况下,您可以执行如下操作:
然后,只需将这些 TableColumns 绑定到 ArrayController.arrangedObjects 并插入适当的值转换器子类。现在,您将无法编辑这些值,但是编辑聚合值意味着什么?
希望有帮助。我已经多次使用这种方法,它肯定比放弃绑定更好。
I encountered the same errors you did. It seems that while you can get the set of employees and perform some set of aggregate operations on it by doing something like:
Department* dept = ;
NSSet* employees = dept.employees;
NSNumber* sumOfHoursWorked = [employees valueForKeyPath: @"@sum.hoursWorked"];
There's a difference when you bind. Bindings are asking to observe the key path, not evaluate it once. Given that, it kinda, sorta makes sense why you can't bind to these key paths. Kinda. Sorta.
Now. As for a solution, what I usually do in cases like this is write a tiny little NSValueTransformer subclass to do just what I need, and then plug that into IB. This way, I write the ten lines of code I need, but don't end up doing the whole NSTableView data source spiel for want of a simple aggregate. In this case, you might do something like this:
Then, just bind those TableColumns to ArrayController.arrangedObjects and plug in the appropriate value transformer subclass. Now, you won't be able to edit those values, but what would it mean to edit an aggregate value anyway?
Hope that helps. I've used this approach a bunch, and it sure beats giving up on bindings.