我有一个由字典数组组成的可变数组:
NSArray *myArray
({
{
"COL_1" = Jhon;
"COL_2" = 01/27/13;
"COL_3" = THAILAND;
"COL_4" = 5000;
"COL_5" = No;
},
{
"COL_1" = Peter;
"COL_2" = 01/27/13;
"COL_3" = US;
"COL_4" = 4000;
"COL_5" = No;
}
})
数组中的所有字典都具有相同的键。我想用一组来自 NSArray 的新密钥替换现有密钥。最终的数组应该像 -
NSArray *myArray
({
{
"A" = Jhon;
"B" = 01/27/13;
"C" = THAILAND;
"D" = 5000;
"E" = No;
},
{
"A" = Peter;
"B" = 01/27/13;
"C" = US;
"D" = 4000;
"E" = No;
}
})
我不想要一个嵌套的 for 循环,在该循环中我必须遍历数组中的每个元素,并再次遍历每个字典,遍历每个键,以更改键。我所做的如下:
NSArray *myDictFnalKeys = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",nil]; // Here we have set our own keys which will replace the existing keys .
NSMutableArray *myNewArray = [[[NSMutableArray alloc] init] autorelease]; //create a new mutable dictionary
for (id myArrayInstance in array) {
NSDictionary *myNewDict = [[NSDictionary alloc] initWithObjects:[myArrayInstance allObjects] forKeys:myDictFnalKeys]; // copy the object values from old dictionary and keys from the new array.
[myNewArray addObject:myNewDict];
}
问题是字典的值没有按正确的顺序排列。[myArrayInstance allObjects] 没有返回正确的顺序,因此最终数组的顺序不正确。它的到来就像-
NSMutableArray *newArray
({
{
A = "01/27/13";
B = No;
C = Jhon;
D = 5000;
E = THAILAND;
},
{
A = "01/27/13";
B = No;
C = Peter;
D = 4000;
E = US;
}
})