Ric*_*kiG 1 cocoa nsdictionary nspredicate
我有一个NSDictionary对象数组.这些字典是从JSON文件解析的.NSDictionary中的所有值对象都是NSString类型,一个键称为"distanceInMeters".
我曾计划使用NSPredicate过滤这些数组,所以我开始这样:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(distanceInMeters <= %f)", newValue];
NSArray *newArray = [oldArray filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
我相信如果"distanceInMeters"键的值是NSNumber,这会有效,但因为我从JSON文件中得到它,所以一切都是NSStrings.上面给出了这个错误:****** - [NSCFNumber length]:无法识别的选择器发送到实例0x3936f00***
这是有道理的,因为我刚刚尝试将NSString视为NSNumber.
有没有办法在字典过滤时从字典中转换值,或者可能采用完全不同的方式来解决这个问题?
希望可以有人帮帮我 :)
试试这个:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(distanceInMeters.floatValue <= %f)", newValue];
Run Code Online (Sandbox Code Playgroud)