gpt4 book ai didi

ios - 使用 NSEqualToPredicateOperatorType 时如何使谓词忽略小数点?

转载 作者:行者123 更新时间:2023-11-28 22:02:03 25 4
gpt4 key购买 nike

在我的应用程序中,我使用谓词来过滤字典数组中的值。

下面是我的数组结构

    (     
{
Apr = "6070.3869";
Feb = "9282.4067";
Jan = "5367.6783";
Jun = "7277.0928";
Mar = "8036.672";
May = "7677.0083";
};
{
Apr = "8542.590200000001";
Feb = "7471.3059";
Jan = "8427.397999999999";
Jun = "9138.6986";
Mar = "7830.722";
May = "9007.052600000001";
};
.
.
.
.
.
)

我使用 TableView 来显示过滤后的列表

如果用户选择属性(来自 jan、feb、mar、Apr)、运算符(>、<、+)并输入 int 值,那么我将显示过滤后的数组

例如,如果他选择 Apr 并输入 7000 并选择 < 运算符,那么我将显示 Apr 键的值小于 7000 的字典数组

我使用下面的谓词来过滤数组

NSPredicate *predicate;
///// filter is an instance of nsobject subclass ho;ding the value, key and operator
NSExpression *lhs = [NSExpression expressionForKeyPath:filter.name];
NSExpression *rhs = [NSExpression expressionForConstantValue:filter.values[0]];
predicate=[NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:[[filter operatorType] intValue]
options:NSCaseInsensitivePredicateOption|NSCaseInsensitiveSearch];

上述方法不适用于 NSEqualToPredicateOperatorType,因为对象是 float

我正在寻找一种在过滤时忽略小数点的方法。为此提出解决方案。

最佳答案

当用户需要 NSEqualToPredicateOperatorType 时,您可以创建复合谓词,其中包含两个定义上限值和下限值的谓词。对于下限,您应该使用用户输入的内容,对于上限 userInput+1

NSExpression *lhs = [NSExpression expressionForKeyPath:filter.name];
NSExpression *rhs = [NSExpression expressionForConstantValue:filter.values[0]];
NSExpression *rhsUpper = [NSExpression expressionForConstantValue:(filter.values[0]+1)]; // assumed that you have number type here
NSPredicate *lowerBoundPredicate =
[NSComparisonPredicate predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSGreaterThanOrEqualToPredicateOperatorType
options:NSCaseInsensitivePredicateOption|NSCaseInsensitiveSearch];
NSPredicate *upperBoundPredicate =
[NSComparisonPredicate predicateWithLeftExpression:lhs
rightExpression:rhsUpper
modifier:NSDirectPredicateModifier
type:NSLessThanPredicateOperatorType
options:NSCaseInsensitivePredicateOption|NSCaseInsensitiveSearch];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[lowerBoundPredicate, upperBoundPredicate]];

复合谓词将返回整数部分等于用户输入的所有小数。

关于ios - 使用 NSEqualToPredicateOperatorType 时如何使谓词忽略小数点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888086/

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