NSPredicate, NsNumber numberWithFloat:0.0 (iPhone)
Asked Answered
D

4

8

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 ?

Dianndianna answered 29/9, 2010 at 16:34 Comment(4)
Please post how you're creating the predicate.Marchall
Please provide the code you use to make the predicate.Legist
predicate:[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue < %@) AND (someId == %@)",startNSNumber,endNSNumber, idString]Dianndianna
maybe we need to have a bigger view of the problem to help, can you provide more lines of code: especially where startNSNumber, endNSNumber and iString come from?Consequence
P
5

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

[NSPredicate predicateWithFormat:@"(startValue => %f) AND (endValue <= %f) AND (someId == %@)",startNSNumber,endNSNumber, idString];
Paradiddle answered 29/9, 2010 at 16:40 Comment(2)
%f is used for a float primitive. NSNumber is an object (which means that %@ is the correct substitution for it).Marchall
Blast, I misread the question and thought he was adding floats :( </shame>Paradiddle
C
2

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];
Choker answered 29/9, 2010 at 19:10 Comment(2)
yeah -.-' my problem is when num1 or num2 is [NSNumber numberWithFloat:0.0] but this value is possible and necessary, but NSPredicate doesn't like it. throws exceptions. why?!Dianndianna
that is quite surprising, are you sure the variable is not nil? I've run a test code with a break point and [NSNumber numberWithFloat:0.0] generates a proper NSNumber object...Choker
M
1

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.

Marchall answered 29/9, 2010 at 16:41 Comment(1)
so if (startNSNumber) { [NSPredicate predicateWithFormat:@"(startValue => %@), startNSNumber] } else { [NSPredicate predicateWithFormat:@"(startValue => %@), [NSNull null] } ?? will it be right ?!Dianndianna
N
0

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.

Newark answered 5/10, 2013 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.