2013-12-11 33 views
-2

裏面我有一個NSMutablearray這樣如何添加新鍵值PAIRE到同一個對象NSMutable陣列

`

(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"; 
}, 

`

我如何添加一個新的鍵值PAIRE這個現有NSMutablearray的對象。我必須通過檢查每個id與另一個字符串來添加此值。

請告訴我如何做到這一點。 感謝

+0

你試過了什麼? –

+0

我試圖通過forloop獲取每個對象,並添加到一個NSMutable數組,但我不知道如何將這個鍵值paire添加到同一個對象內,它作爲另一個對象添加到我的NSMutable數組 – user2889249

+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]; 
} 
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]; 
    } 
+0

這是要取代什麼? – user2889249

+0

如果您嘗試將'NSDictionary'對象轉換爲'NSMutableDictionary',則此代碼可能會'crash'... – Mani

+0

@ user2889249這將添加一個更多的鍵值對。 –

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; 
相關問題