我有一个 NSManagedObject Organization,它与 NSManagedObject Employee 有一对多的关系。
//to-many relationship from Organisation to Employee
@property (nonatomic, retain) NSSet *employees;
//Employee has a property named age of type NSNumber
@property (nonatomic, retain) NSNumber *age;
以下代码行给出组织内的最大年龄:
NSNumber *maxAge = [organisation.employees valueForKeyPath:@"@max.age"];
我需要了解 valueForKeyPath:@"@max.age"
是如何找到最大年龄的。
我不确定@max
是NSSet还是Core Data自己提供的。
最佳答案
这归功于称为键值编码的功能。具体来说,@max 是一个“集合运算符”。其他示例是@sum、@avg、@count。 CoreData 支持 KVC,但 NSArray
和 NSSet
等其他集合也支持 KVC。 Apple 文档 here解释:
“集合运算符是作为参数传递给 valueForKeyPath: 方法的专用键路径。运算符由前面带有 @ 符号 (@) 的字符串指定。集合运算符左侧的键路径,如果存在,则确定操作中使用的相对于接收器的数组或集合。运算符右侧的键路径指定运算符使用的集合的属性。”
关于ios - 聚合函数如何处理 Core Data 中的一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545704/