2011-04-24 42 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