我有一个 NSDictionary 对象数组。这些字典是从 JSON 文件中解析出来的。NSDictionary 中的所有值对象都是 NSString 类型,一个键称为“distanceInMeters”。
我曾计划使用 NSPredicate 过滤这些数组,所以我开始时是这样的:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(distanceInMeters <= %f)", newValue];
NSArray *newArray = [oldArray filteredArrayUsingPredicate:predicate];
如果“distanceInMeters”键的值是 NSNumber,我相信这会起作用,但是因为我从 JSON 文件中得到它,所以一切都是 NSStrings。上面给出了这个错误:****** -[NSCFNumber length]: unrecognized selector sent to instance 0x3936f00***
这是有道理的,因为我刚刚尝试将 NSString 视为 NSNumber。
有没有办法在过滤字典时从字典中转换值,或者可能有一种完全不同的方法来解决这个问题?
希望可以有人帮帮我 :)