如何将新键值对添加到NSMutablearray内的同一对象中

时间:2013-12-11 06:36:24

标签: ios nsmutablearray

我有NSMutablearray这样的

`

(NSMutableArray *) $4 = 0x1d558bb0 <__NSArrayM 0x1d558bb0>(
{
id = 4;
name = "Affection Songs";
noOfSongs = 11;
},
{
id = 11;
name = "Aurudu Songs";
noOfSongs = 7;
},
{
id = 2;
name = "Birth Day Songs";
noOfSongs = 1;
},
{
id = 41;
name = "Broken heart song";
noOfSongs = 85;
},

`

我想要做的是为每个对象添加另一个键值对。所以我的新NSMutable数组应该是这样的

`

(NSMutableArray *) $4 = 0x1d558bb0 <__NSArrayM 0x1d558bb0>(
{
id = 4;
name = "Affection Songs";
noOfSongs = 11;
status = "Check";
},
{
id = 11;
name = "Aurudu Songs";
noOfSongs = 7;
status = "UnCheck";
},
{
id = 2;
name = "Birth Day Songs";
noOfSongs = 1;
status = "Check";
},
{
id = 41;
name = "Broken heart song";
noOfSongs = 85;
status = "Check";
},

`

如何为现有的NSMutablearray对象添加新的键值对。我必须通过用另一个字符串检查每个id来添加这个值。

请告诉我怎么做。 感谢

3 个答案:

答案 0 :(得分:2)

尝试使用以下代码:

for(int i=0;i<[YourArray count];i++)
{
     NSMutableDictionary *objectDict=[[NSMutableDictionary alloc] initWithDictionary:[YourArray objectAtIndex:i]];
     [objectDict setObject:@"status value" forKey:@"status"];
     [YourArray replaceObjectAtIndex:i withObject:objectDict];
}

答案 1 :(得分:2)

我希望这段代码可以帮到你。

    NSMutableArray *newArray =  [NSMutableArray array];
    for (NSDictionary *subDict in yourArray)
    {
     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:subDict];
    [dict setObject:[self yourMethodToGetStatusFor:subDict] forKey:@"status"];
    [newArray addObject:dict];
    }
    self.yourArray = newArray;

答案 2 :(得分:0)

你可以试试这个:

  for (int i = 0; i < [mutableArray count]; i++) {

        NSMutableDictionary *dict = [[mutableArray objectAtIndex:i]mutableCopy];
        [dict setValue:@"YOUR_VALUE" forKey:@"status"];
        [mutableArray replaceObjectAtIndex:i withObject:dict];
    }