Jai*_*min 3 iphone objective-c
我有一个json响应即将到来,我需要获取其键是特定字符串的所有值...例如:www_name,www_age等作为键出现在nsmutabledictionary中我现在想要搜索所有那些具有"www_"的值他们的一部分.
ken*_*ytm 21
循环遍历字典并过滤.
NSMutableArray* result = [NSMutableArray array];
for (NSString* key in dictionary) {
if ([key hasPrefix:@"www_"]) {
[result addObject:[dictionary objectForKey:key]];
// write to a dictionary instead of an array
// if you want to keep the keys too.
}
}
return result;
Run Code Online (Sandbox Code Playgroud)
Dan*_*son 13
您也可以要求字典为您过滤结果并使用NSPredicate返回数组,而不是自己迭代集合.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith www_"];
NSArray *filtered = [[dictionary allKeys] filteredArrayUsingPredicate:predicate];
Run Code Online (Sandbox Code Playgroud)
只是一个想法.