如何使用NSPredicate从字典数组中仅过滤特定键值

时间:2012-12-21 10:44:14

标签: objective-c

NSArray *details = [NSArray arrayWithObjects:@"name",@"age",@"gender",nil];
NSArray *ray1  = @[@"ray",@"23",@"male"];
NSArray *steve1  = @[@"steve",@"23",@"male"];
NSDictionary *ray = [NSDictionary dictionaryWithObjects:ray1 forKeys:details];
NSDictionary *steve = [NSDictionary dictionaryWithObjects:steve1 forKeys:details];
NSArray *register = @[ray,steve];

我需要使用NSPredicate

仅过滤密钥的值

1 个答案:

答案 0 :(得分:1)

这将打印所有词典的键“name”的值

NSArray *details = [NSArray arrayWithObjects:@"name",@"age",@"gender",nil];
NSArray *ray1  = @[@"ray",@"23",@"male"];
NSArray *steve1  = @[@"steve",@"23",@"male"];
NSDictionary *ray = [NSDictionary dictionaryWithObjects:ray1 forKeys:details];
NSDictionary *steve = [NSDictionary dictionaryWithObjects:steve1 forKeys:details];
NSArray *reg = @[ray,steve];
NSPredicate *pr = [NSPredicate predicateWithValue:YES];
NSArray *ar = [[reg valueForKey:@"name"] filteredArrayUsingPredicate:pr];
NSLog(@"%@",ar);