gpt4 book ai didi

iOS:在核心数据谓词中使用@min 和@max

转载 作者:可可西里 更新时间:2023-11-01 03:49:52 24 4
gpt4 key购买 nike

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

我正在使用以下 NSPredicate:

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

但是,我收到以下错误:

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

哪里做错了?

最佳答案

我认为 NSPredicate 不支持这样的函数,除非它是 bool 谓词表达式的一部分(即涉及“大于”之类的东西)。

你应该阅读 this CoreData documentation其中给出了一些例子,具体以max为例:

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com