NSPredicate、NsNumber numberWithFloat:0.0 (iPhone)

发布于 2024-09-25 14:13:23 字数 338 浏览 2 评论 0原文

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

创建谓词:

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

我该如何解决这个问题?

hi i have Core Data database with numerical attributes. they are NSNumbers. Default value is 0.0 but when i try to do some NSPredicated fetch, i get "'NSInvalidArgumentException', reason: 'Invalid predicate: nil RHS'" just because attribute value is 0.0

The predicate is created with:

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

how can i solve this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔日梦未散 2024-10-02 14:13:23

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

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

You're adding the floats as objects, not as floats? Try this :

[NSPredicate predicateWithFormat:@"(startValue => %f) AND (endValue <= %f) AND (someId == %@)",startNSNumber,endNSNumber, idString];
撩发小公举 2024-10-02 14:13:23

我坚信您的变量之一为零或已自动释放...

尝试以下操作:

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];

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

如果您希望有时 num1num2nil,那么您可以将谓词重写为:

[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue <= %@) AND (someId == %@)", num1 ? num1 : [NSNumber numberWithFloat:0.0], num2 ? num2 : [NSNumber numberWithFloat:0.0], idString];

I strongly believe one of your variables is nil or was autoreleased...

try this:

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];

If this succeeds that the problem is with your variables.

If you expect either num1 or num2 to be nil sometimes, then you could rewrite the predicate as:

[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue <= %@) AND (someId == %@)", num1 ? num1 : [NSNumber numberWithFloat:0.0], num2 ? num2 : [NSNumber numberWithFloat:0.0], idString];
梦初启 2024-10-02 14:13:23

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

startNSNumberendNSNumberidString 为 nil。如果您想将 NSPredicate 中的某些内容与 nil 进行比较,则需要替换为 [NSNull null],而不是 nil代码>.

Well, given the predicate you provided, the easy answer is:

Either startNSNumber, endNSNumber or idString is nil. If you want to compare something against nil in an NSPredicate, you need to substitute in [NSNull null], not nil.

感情废物 2024-10-02 14:13:23

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

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

我认为这篇文章很有用。

IN NSpredicate use string value as float the Dictionary key.floatValue and easily use.

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

I thing this post useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文