将键值函数与 NSExpression 结合起来

发布于 2024-12-02 15:42:59 字数 1029 浏览 0 评论 0原文

我可以将键值收集函数与 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 技术交流群。

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

发布评论

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

评论(1

人事已非 2024-12-09 15:42:59

我这样做:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"usageMetrics.value"]; 
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

它返回一个包含 12 个结果的 NSArray(这是它找到的托管对象的数量),并且对这些值进行了求和。

然后我就这么做了:

NSDecimalNumber *maxValue = [results valueForKeyPath:@"@max.maxValue"]; (maxValue being the name of the NSExpressionDescription)

这有效!

我想知道是否可以将 sum:max: 堆叠到 NSExpression 中。

I do this:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"usageMetrics.value"]; 
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

which returns an NSArray of 12 results (this is how many managed objects it found) and it has summed the values.

I then do:

NSDecimalNumber *maxValue = [results valueForKeyPath:@"@max.maxValue"]; (maxValue being the name of the NSExpressionDescription)

which worked!

I would like to know if I could stack the sum: and the max: into an NSExpression.

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