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 ?

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
Greg
  • 83
  • 2
  • 4
  • 1
    Please post how you're creating the predicate. – Dave DeLong Sep 29 '10 at 16:36
  • Please provide the code you use to make the predicate. – ohhorob Sep 29 '10 at 16:37
  • predicate:[NSPredicate predicateWithFormat:@"(startValue => %@) AND (endValue < %@) AND (someId == %@)",startNSNumber,endNSNumber, idString] – Greg Sep 29 '10 at 16:39
  • 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? – Mick F May 22 '12 at 10:10

4 Answers4

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];
Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
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];
Nick
  • 2,626
  • 1
  • 26
  • 29
  • 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 Sep 29 '10 at 20:19
  • 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 Sep 29 '10 at 21:08
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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • so if (startNSNumber) { [NSPredicate predicateWithFormat:@"(startValue => %@), startNSNumber] } else { [NSPredicate predicateWithFormat:@"(startValue => %@), [NSNull null] } ?? will it be right ?! – Greg Sep 29 '10 at 16:47
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.

PeterParker
  • 308
  • 4
  • 8