[英]when i remove the values from array after adding in to the dictionary.The array values are removed from the dictionary in iOS?
我将数组添加到字典中,然后从数组中删除所有对象。当我打印字典时,它也显示为空。表示字典中存在的数组数据也会删除。
if ([[[testMenuGroupAry objectAtIndex:0]objectAtIndex:group] isEqualToString:[filteredAry objectAtIndex:objCount]])
{
NSLog(@"object count is %d",objCount);
[groupAry addObject:[array1 objectAtIndex:group]];
}
[tempDict setValue:groupAry forKey:[filteredAry objectAtIndex:objCount]];
[groupAry removeAllObjects];
NSLog(@"before Dictionary temp Dict : %@",tempDict);
判决也显示空洞。为什么?
因为你删除了它的所有元素。 将对象添加到集合不会复制它们,它只保留实例。
发生这种情况是因为您的字典仅存储对数组的引用。 它不会复制整个数组。 您可以做的是先复制数组,然后将其存储在字典中。
改变这行代码:
[groupAry addObject:[array1 objectAtIndex:group]];
至
[groupAry addObject:[[array1 objectAtIndex:group] mutableCopy]];
这将有助于保留对象,并且您的数组不会变为零。
如果你想要判刑不是空的。 你应该这样做:[tempDict setValue:[NSMutableArray arrayWithArray:groupAry] forKey:[filteredAry objectAtIndex:objCount]]; 它制作了groupAry的副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.