将键值函数与 NSExpression 结合起来
我可以将键值收集函数与 NSExpression 结合使用吗?我将它与 CoreData 一起使用来从我的托管对象类中获取值的最大值,并且效果很好。
现在我有另一个托管对象类,其中包含一个 NSSet。我想找到 NSSet
中值的 @sum 的最大值。
例如,我有 10 个托管对象,每个对象都有一个 NSSet 值。我想对每组中的值进行@sum,然后找到 10 个托管对象中最大的一个。
我通常会使用 [object valueForKeyPath:@"[电子邮件受保护]"]
- 工作正常。
我想结合并做类似的事情:
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"[email protected]"];
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
它似乎不起作用
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath element (not a relationship or attribute): @sum'
Can I combine key value collection functions with NSExpression
? I was using it with CoreData to get the max: of a value from my managed object classes, and it was working great.
Now i have this other managed object class which contains an NSSet
. I want to find the max: of the @sum of values in the NSSet
.
So for example, I have 10 managed objects, each with an NSSet of values. I want to @sum the values in each set, and then find the largest one of the 10 managed objects.
I would usually get the sum using [object valueForKeyPath:@"[email protected]"]
- which works fine.
I want to combine and do somethinge like:
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"[email protected]"];
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
It doesn't seem to be working
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath element (not a relationship or attribute): @sum'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我这样做:
它返回一个包含 12 个结果的 NSArray(这是它找到的托管对象的数量),并且对这些值进行了求和。
然后我就这么做了:
这有效!
我想知道是否可以将
sum:
和max:
堆叠到NSExpression
中。I do this:
which returns an NSArray of 12 results (this is how many managed objects it found) and it has summed the values.
I then do:
which worked!
I would like to know if I could stack the
sum:
and themax:
into anNSExpression
.