1
votes

I have an Array of NSDictionary objects. These Dictionaries are parsed from a JSON file. All value objects in the NSDictionary are of type NSString, one key is called "distanceInMeters".

I had planned on filtering these arrays using an NSPredicate, so I started out like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(distanceInMeters <= %f)", newValue];
NSArray *newArray = [oldArray filteredArrayUsingPredicate:predicate];

I believe this would have worked if the value for the "distanceInMeters" key was an NSNumber, but because I have it from a JSON file everything is NSStrings. The above gives this error:****** -[NSCFNumber length]: unrecognized selector sent to instance 0x3936f00***

Which makes sense as I had just tried to treat an NSString as an NSNumber.

Is there a way to cast the values from the dictionary while they are being filtered, or maybe a completely different way of getting around this?

Hope someone can help me :)

2

2 Answers

7
votes

Try this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(distanceInMeters.floatValue <= %f)", newValue];
0
votes

Your problem isn't the predicate; your problem is that you're dealing with NSString objects instead of dealing with NSNumber objects. I would focus my time on changing them to NSNumbers first, and then verify that it's not working.

FYI, the JSON Framework does automatic parsing of numbers into NSNumbers...