我想将icdcodeDictionary添加到backlist中,但它会更改backlist的所有元素。在此过程之后,它们与最后添加的项目相同。我该如何解决这个问题?我点了这个链接。http://iphonedevsdk.com/forum/iphone-sdk-development/88286-adding-object-to-nsmutablearray-changes-all-objects.html 但什么都没发生。感谢。
NSMutableDictionary *icdcodeDictionary;
@property (nonatomic, retain) NSMutableArray *backlist;
[self.backlist addObject:icdcodeDictionary];
的结果
NSLog(@“backlist%d index:%@”,self.backlist.count-2,[icdcodeDictionary objectForKey:@“Name”]);
NSLog(@“backlist%d index:%@”,self.backlist.count-1,[icdcodeDictionary objectForKey:@“Name”]):
backlist 0 index: Urlar
backlist 1 index: Urlar
但我想要这个:
backlist 0 index: Neoplazma backlist 1 index: Urlar
答案 0 :(得分:1)
它与您正在打印的字典相同。您需要为每个addObject:
创建一个新的。
[self.backlist addObject:[NSMutableDictionary dictionaryWithDictionary:icdcodeDictionary]];
答案 1 :(得分:0)
好的,当你在for循环中设置字典时我遇到了你的问题你需要在你的for循环中写下面这样的代码。
// here it is for loop
for (i=0; i<2; i++)
{
// this dictinary needs to call how many times
you set object
NSMutableDictionary * mut=
[NSMutableDictionary dictionary];
// now set your object inside dictionary
}