冒昧地创建了一个初始数组来工作......
NSArray* allKeys = @[@"key1",@"key2",@"key3",@"key4",@"key5"];
NSArray* allObjs = @[@"a",@"b",@"c",@"d",@"e"];
NSMutableArray* originalArray = [NSMutableArray arrayWithCapacity:4];
for (int i=0; i<4; i++) {
NSRange subRange = NSMakeRange(0, i+1);
[originalArray addObject:[NSDictionary dictionaryWithObjects:[allObjs subarrayWithRange:subRange] forKeys:[allKeys subarrayWithRange:subRange]]];
}
NSLog(@"origianl array = %@",originalArray);
NSArray* keys = @[@"key1",@"key3"];
__block NSMutableArray* newArr = [NSMutableArray arrayWithCapacity:4];
[originalArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[newArr addObject:[NSDictionary dictionaryWithObjects:[obj objectsForKeys:keys notFoundMarker:[NSNull null]] forKeys:keys]];
}];
NSLog(@"new array = %@",newArr);
在之前和之后给出以下内容:
2013-09-16 13:20:14.129 GammonPos[910:11303] origianl array = (
{
key1 = a;
},
{
key1 = a;
key2 = b;
},
{
key1 = a;
key2 = b;
key3 = c;
},
{
key1 = a;
key2 = b;
key3 = c;
key4 = d;
}
)
2013-09-16 13:20:14.131 GammonPos[910:11303] new array = (
{
key1 = a;
key3 = "<null>";
},
{
key1 = a;
key3 = "<null>";
},
{
key1 = a;
key3 = c;
},
{
key1 = a;
key3 = c;
}
)