iOS:在核心数据谓词中使用@min和@max [英] iOS: Using @min and @max in a core data predicate

查看:346
本文介绍了iOS:在核心数据谓词中使用@min和@max的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据实体 Client ,它有一个折扣属性。我要获取最小折扣的客户端。



我使用以下 NSPredicate

  [NSPredicate predicateWithFormat:@@ min.discount];但是,我收到以下错误:


 由于未捕获异常NSInvalidArgumentException而终止应用程序,原因:'无法解析格式字符串@ min.discount'

什么不正确?

解决方案

't think NSPredicate 支持这样的函数,除非它是布尔谓词表达式的一部分(即涉及类似大于的东西)。



您应该阅读此CoreData文档,其中提供了一些示例,具体使用 max 作为示例:


创建和使用表达式
描述的步骤如下。



首先你需要创建表示您感兴趣的值的键路径的表达式( NSExpression 的实例),
表示要应用的函数as max:or min:):

  NSExpression * keyPathExpression = [NSExpression expressionForKeyPath:@salary]; 
NSExpression * maxSalaryExpression = [NSExpression expressionForFunction:@max:
arguments:[NSArray arrayWithObject:keyPathExpression]];

有关支持的函数的完整列表,请参见 expressionForFunction:arguments: code>。



然后创建表达式描述并设置其名称,表达式和结果类型。



名称是将在字典中用于返回值的键。如果要检索多个值(例如
最大值和Employee表中的最小工资) -
的名称对于给定的获取请求,每个表达式描述必须是唯一的。

  NSExpressionDescription * expressionDescription = [[NSExpressionDescription alloc] init]; 
[expressionDescription setName:@maxSalary];
[expressionDescription setExpression:maxSalaryExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];



最后,将请求的属性设置为仅获取由表达式表示的属性:

  [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; 



I have a core data entity, Client, which has a discountproperty. I want to fetch the the client with the smallest discount.

I am using the following NSPredicate:

[NSPredicate predicateWithFormat:@"@min.discount"];

However, I am getting the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "@min.discount"'

What am not doing right?

解决方案

I don't think NSPredicate has support for functions like this unless it is part of a boolean predicate expression (i.e. involving things like "greater than").

You should read this CoreData documentation which gives some examples, specifically using max as an example:

There are a number of steps to follow to create and use the expression description.

First you need to create expressions (instances of NSExpression) to represent the key-path for the value you’re interested in, and to represent the function you want to apply (such as max: or min:):

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"salary"];
NSExpression *maxSalaryExpression = [NSExpression expressionForFunction:@"max:"
                                                  arguments:[NSArray arrayWithObject:keyPathExpression]];

For a full list of supported functions, see expressionForFunction:arguments:.

You then create the expression description and set its name, expression, and result type.

The name is the key that will be used in the dictionary for the return value. If you want to retrieve multiple values—such as the largest and the smallest salaries in an Employee table—the name of each expression description must be unique for a given fetch request.

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxSalary"];
[expressionDescription setExpression:maxSalaryExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];

Finally, you set the request’s properties to fetch just the property represented by the expression:

[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

这篇关于iOS:在核心数据谓词中使用@min和@max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆