Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate issue with greater than equals

I have a simple NSPredicate which is not giving correct result

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.data.squareFootage >= %@", filterSquareFootage];
filteredArray = [[filteredArray filteredArrayUsingPredicate:resultPredicate] mutableCopy];

Strangely this works for all 6000>=250, 7000>=250, 8000>=6000. But as soon as squareFootage==10000 the predicate for 10000>=any smaller value is false. Note: 10000 for squareFootage is a max value fetched using a UISlider.If i reduce value to any smaller value, the predicate gives true as result

Am i missing something here and using predicate incorrectly?

like image 323
Madhur Rawat Avatar asked Aug 02 '13 07:08

Madhur Rawat


1 Answers

I assume that your property is stored as string and not as number, therefore the values are compared as strings:

"10000" < "250" < "6000"

Using NSNumber instead of NSString (or alternatively some scalar type as NSInteger) should fix the problem.

If you cannot change the data type of the squareFootage property, then the following predicate should work:

[NSPredicate predicateWithFormat:@"SELF.data.squareFootage.intValue >= %d", [filterSquareFootage intValue]]
like image 183
Martin R Avatar answered Oct 03 '22 04:10

Martin R