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