views:

45

answers:

3

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 ?

A: 

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

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

deanWombourne
If it is NSNumber, this clearly won't work.
Eiko
`%f` is used for a `float` primitive. `NSNumber` is an object (which means that `%@` is the correct substitution for it).
Dave DeLong
Blast, I misread the question and thought he was adding floats :( </shame>
deanWombourne
A: 

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.

Dave DeLong
so if (startNSNumber) {[NSPredicate predicateWithFormat:@"(startValue => %@), startNSNumber]} else {[NSPredicate predicateWithFormat:@"(startValue => %@), [NSNull null]}?? will it be right ?!
Greg
no it's NSInvalidArgumentException again :/
Greg
A: 

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];
Nick
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?!
Greg
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...
Nick
yes, unfortunately i'm totally sure :(
Greg