开发者

NSPredicate, NsNumber numberWithFloat:0.0 (iPhone)

开发者 https://www.devze.com 2023-01-17 22:26 出处:网络
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

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

开发者_StackOverflow

The predicate is created with:

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

how can i solve this problem ?


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

[NSPredicate predicateWithFormat:@"(startValue => %f) AND (endValue <= %f) AND (someId == %@)",startNSNumber,endNSNumber, 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];


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消