【发布时间】:2015-06-20 23:48:00
【问题描述】:
我正在遍历一个数组,获取两个项目并将它们放入字典中,然后将它们添加到一个数组中,但它会不断为数组中的数字对象添加数组的最后一项,即使当我在循环中记录字典,它具有正确的值。这是我的代码
for (TFHppleElement *element in nodesArray) {
[nameAndHrefDict setObject:[element objectForKey:@"href"] forKey:@"href"];
[nameAndHrefDict setObject:[element text] forKey:@"name"];
NSLog(@"PRE: %@", nameAndHrefDict);
[arrayOfDicts addObject:nameAndHrefDict];
NSLog(@"IN: %@", arrayOfDicts);
}
在日志中我看到了这个
PRE: {
href = "/dining-choices/res/index.html";
name = "Commons Dining Hall";
}
IN: (
{
href = "/dining-choices/res/index.html";
name = "Commons Dining Hall";
}
PRE: {
href = "/dining-choices/res/sage.html";
name = "Russell Sage Dining Hall";
}
IN: (
{
href = "/dining-choices/res/sage.html";
name = "Russell Sage Dining Hall";
},
{
href = "/dining-choices/res/sage.html";
name = "Russell Sage Dining Hall";
}
)
发生了什么?
但是它添加了8次nodesArray的最后一个值,而不是每个值,为什么?
提前感谢您的帮助。
【问题讨论】:
-
您多次添加相同的字典。 当然你会得到多份!
标签: ios objective-c for-loop nsarray nsdictionary