使用Objective-C从NSArray创建NSMutableArray

7

我有一个NSArray,其中包含以下数据:

01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
         {
         "id_acompanhante" = "";
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = "Himself";
         name = iUser;
         status = done;
         type = User;
     }
         {
         "id_acompanhante" = 1;
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = iUser;
         name = "Mayara Roca";
         status = naofeito;
         type = companion;
     }
)

如何将这些数据添加到NSMutableArray中,并在每个项目中添加另一个字段。例如:
  {
     "id_acompanhante" = "";
     "id_evento" = 34;
     "user_id" = 1;
     "inserido_por" = "Himself";
     name = IUser;
     status = done;
     type = User;
     tag = 2;
 }

我添加了一个名为“TAG”的字段。
我该如何做?

花括号内的所有内容是你的数组的内容,还是代表单个数组。 - Souljacker
我在我的回答中放入了代码,用于为数组中的每个字典添加一个TAG字段。 - Fogmeister
如果您要将一个值添加到字典中的键“TAG”中,那么您应该采用Fogmeister的答案。 - Souljacker
6个回答

8
NSMutableArray *mutableArray = [NSMutableArray array];

for (NSDictionary *dictionary in yourArray) {
    NSMutableDictionary *mutDictionary = [dictionary mutableCopy];

    [mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"];

    [mutableArray addObject:mutDictionary];
}

这是如何完成您所要求的操作。将数组变为可变类型并不能让您改变其中字典的内容。您需要将每个字典单独取出来,将其变为可变类型,然后再存回数组中。


7

简单易懂

NSMutableArray *mArray = [NSMutableArray arrayWithArray:yourArray]
[mArray addObject:yourObject];

这不是被问到的内容。 - Fogmeister

1

只需在数组上调用mutableCopy,就像这样简单:

NSArray * myArray = @[@"one", @"two", @"three"];
NSMutableArray * myMut = [myArray mutableCopy];
NSLog(@"My Mut = %@", myMut);

希望这能帮到你!

更新 以下是一个示例,它遍历您的原始数组,将项目转换为可变字典并插入新属性。然后,您放弃原始数组并使用myMut版本。

NSArray * myArray = @[@{@"key1":@"value1"}, @{@"key2":@"value2"}];
NSMutableArray * myMut = [[NSMutableArray alloc] init];
for (NSDictionary * dict in myArray) {
    NSMutableDictionary * mutDict = [dict mutableCopy];
    [mutDict setObject:@"2" forKey:@"tag"]; // Add new properties
    [myMut addObject:mutDict];
}

我的错,我没有完全阅读你的问题。我的答案将让你将数组转换为可变的,但看起来你有一个包含NSDictionaries的数组。要向字典添加属性,你需要使字典变成可变的。 - Dave

0

试试这个

NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray];

0
据我所知,您不需要一个可变数组,但您需要将数组中的NSDictionaries转换为可变。其他人也回答了这个问题。
但是我想展示一个巧妙的技巧,如何在一行中向所有可变字典添加键/值对。我使用比您更简单的数据,以便更容易看到发生了什么。
//just an example array of mutable Dictionary
NSMutableArray *array = [NSMutableArray array];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"one" forKey:@(1)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"two" forKey:@(2)]];
[array addObject:[NSMutableDictionary dictionaryWithObject:@"three" forKey:@(3)]];

// and here is the trick
[array setValue:@"10" forKey:@"tag"];

数组现在包含

(
{
    tag = 10;
    1 = one;
},
{
    tag = 10;
    2 = two;
},
{
    3 = three;
    tag = 10;
}
)

NSArray文档中的内容

setValue:forKey:
Invokes setValue:forKey: on each of the array's items using the specified value and key.

- (void)setValue:(id)value forKey:(NSString *)key

0
NSArray *theOldArray; // your old array
NSMutableArray *theMutableAry = [NSMutableArray array];
for (NSDictionary *dicItem in theOldArray)
{
    NSMutableDictionary *dicWithOneMoreField = [NSMutableDictionary dictionaryWithDictionary:dicItem];
    [dicWithOneMoreField setValue:@"your value for tag" forKey:@"tag"];
    [theMutableAry addObject:dicWithOneMoreField];
}

或者尝试另一种方式

NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:theOldArray];
NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
for (NSDictionary *dicItem in mutableArray)
{
    [dicItem setValue:@"your value for tag" forKey:@"tag"];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接