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”。

我该怎么做呢?

4

6 回答 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];
}

这就是你问的方法。使数组可变将不允许您更改其中字典的内容。您需要取出每个字典并使其可变,然后将其存储回数组中。

于 2013-01-14T15:51:29.260 回答
7

简单的

NSMutableArray *mArray = [NSMutableArray arrayWithArray:yourArray]
[mArray addObject:yourObject];
于 2013-01-14T15:37:13.177 回答
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];
}
于 2013-01-14T15:43:35.863 回答
0

试试这个

NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray];
于 2013-01-14T15:38:10.027 回答
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:
使用指定的值和键对数组的每个项目调用 setValue:forKey:。

- (void)setValue:(id)value forKey:(NSString *)key
于 2013-01-14T16:20:08.410 回答
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"];
}
于 2013-01-17T08:37:07.663 回答