8

嗨,我有带有数字属性的核心数据数据库。他们是 NSNumbers。默认值为 0.0,但是当我尝试进行一些 NSPredicated 提取时,我得到“'NSInvalidArgumentException',原因:'无效谓词:nil RHS'”只是因为属性值为 0.0

谓词是通过以下方式创建的:

[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue <= %@) AND (someId == %@)",startNSNumber,endNSNumber, idString]

我怎么解决这个问题 ?

4

4 回答 4

5

您将浮动添加为对象,而不是浮动?尝试这个 :

[NSPredicate predicateWithFormat:@"(startValue => %f) AND (endValue <= %f) AND (someId == %@)",startNSNumber,endNSNumber, idString];
于 2010-09-29T16:40:33.980 回答
2

我坚信您的变量之一是 nil 或被自动释放...

尝试这个:

NSNumber *num1 = [NSNumber numberWithFloat:2.0];
NSNumber *num2 = [NSNumber numberWithFloat:3.0];
NSString *str = @"Test";
[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue <= %@) AND (someId == %@)", num1, num2, str];

如果成功,则问题出在您的变量上。

如果您希望有时是num1或有时num2nil,那么您可以将谓词重写为:

[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue <= %@) AND (someId == %@)", num1 ? num1 : [NSNumber numberWithFloat:0.0], num2 ? num2 : [NSNumber numberWithFloat:0.0], idString];
于 2010-09-29T19:10:16.690 回答
1

好吧,鉴于您提供的谓词,简单的答案是:

要么startNSNumberendNSNumber要么idString为零。如果您想将某物与nilin进行比较NSPredicate,则需要替换为 in [NSNull null],而不是nil

于 2010-09-29T16:41:43.973 回答
0

IN NSpredicate 使用字符串值作为浮动字典 key.floatValue 并轻松使用。

[NSPredicate predicateWithFormat:@"(startValue.floatValue => %f) AND (endValue.floatValue <= %f) AND (someId == %@)",startNSNumber,endNSNumber, idString]

我觉得这篇文章很有用。

于 2013-10-05T13:06:31.657 回答