2011-04-24 55 views
1

我有一個字典項目的NSMutableArray(即「故事」)。更新字典對象的NSMutableArray

我可以很好地讀取數組中的字典項目,例如

[[[stories objectAtIndex: b] objectForKey: @"title"] 

但是,現在我試圖更新(即替換)一些對象,例如, 「title」&「matchingword」,但我找不到合適的代碼。任何建議,非常感謝。

我想這一點,但它似乎是增加全新的對象數組

NSMutableDictionary *itemAtIndex = [[NSMutableDictionary alloc]init]; 
[itemAtIndex setObject:[[placesArray objectAtIndex:a]objectAtIndex:0] forKey:@"reference"]; 
[stories replaceObjectAtIndex:x withObject:itemAtIndex]; // replace "reference" with user's unique key 
[itemAtIndex release]; 

我也試過這個(但沒有工作,要麼):

//NSMutableDictionary *itemAtIndex2 = [[NSMutableDictionary alloc]init]; 
//[itemAtIndex2 setObject:[separatePlaces objectAtIndex:x] forKey:@"matchingword"]; 
//[stories insertObject:itemAtIndex2 atIndex:x]; // add the unique matching word to story 
//[itemAtIndex2 release]; 

幫助表示讚賞。謝謝。

回答

2

你需要抓住你想要修改的字典。

NSMutableDictionary *temp = [stories objectAtIndex: b]; 

的變化值:

[temp setObject:@"new Info" forKey:@"title"]; 
+0

謝謝。事實證明這有點複雜。由於我不明白的原因,上面在運行時創建了一個錯誤,說我試圖更改一個不可變對象。我檢查了數組中的所有內容都是可變的。所以我補充說:tempDict = [[stories objectAtIndex:b] mutableCopy];我更新了對象,但他們不會保存到第一個字典中,因此將結果複製到新的字典中。這很好。謝謝。 – Jeremy 2011-04-24 11:14:43