8

我有一个简单的 NSPredicate 没有给出正确的结果

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.data.squareFootage >= %@", filterSquareFootage];
filteredArray = [[filteredArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];

奇怪的是,这适用于所有 6000>=250、7000>=250、8000>=6000。但是一旦 squareFootage==10000 10000>=任何较小的值的谓词为假。注意:squareFootage 的 10000 是使用 UISlider 获取的最大值。如果我将值减小到任何较小的值,则谓词给出 true 作为结果

我在这里遗漏了什么并且错误地使用了谓词吗?

4

2 回答 2

22

我假设您的属性存储为string而不是number,因此将值作为字符串进行比较:

"10000" < "250" < "6000"

使用NSNumberinstead of NSString(或者一些标量类型 as NSInteger)应该可以解决问题。

如果您无法更改squareFootage属性的数据类型,则以下谓词应该有效:

[NSPredicate predicateWithFormat:@"SELF.data.squareFootage.intValue >= %d", [filterSquareFootage intValue]]
于 2013-08-02T07:17:29.377 回答
0

您不能使用 =< 比较字符串。如果 SELF.data.squareFootage 是 NSNumber,请尝试将 filterSquareFootage 转换为 NSnumber 或 int 值并像这样比较它们。

于 2013-08-02T07:20:07.240 回答