ios - 我将数组添加到字典中,然后从数组中删除所有对象

标签 ios nsarray nsmutabledictionary

我将数组添加到字典中,然后从数组中删除所有对象。 当我打印字典时,它也显示为空。意味着字典中存在的数组数据也被删除。

if ([[[array objectAtIndex:0]objectAtIndex:i] isEqualToString:[filteredAry objectAtIndex:j]])
{
    [menuAry addObject:[array1 objectAtIndex:i]];
}

[tempDict setValue:menuAry forKey:[filteredAry objectAtIndex:j]];
[menuAry removeAllObjects];

为什么 tempdict 显示为空?

最佳答案

您要添加到字典中的数组与您要从中删除所有对象的数组是同一对象。将其添加到字典中并不会创建新对象,它只是添加对同一对象的引用。

如果你想将它们分开,你应该向字典添加一个副本,如下所示:

NSMutableArray *copiedArray = [[menuAry mutableCopy] autorelease];
[tempDict setObject:copiedArray forKey:[filteredAry objectAtIndex:j]];
//...

如果您使用 ARC,则可以省略 autorelease

关于ios - 我将数组添加到字典中,然后从数组中删除所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637784/

相关文章:

ios - 向 UITableViewController 内的表格 View 添加左右边距

objective-c - iOS 比较数组和集合

ios - 错误消息 : Incompatible pointer to integer conversion sending 'NSArray *__strong' to parameter of type 'NSUInteger'

ios - 无法将类型 'Data' 的值转换为预期的参数类型 'Data'

nsdictionary - 具有容量的 NSMutableDictionary

iphone - 如何根据存储在其中的NSString重新排序NSMutable数组?

iphone - 有网页开发经验。如何开始进行iPhone游戏开发?

iOS SpriteKit : applyForce to physics body totally different effect depending on device model

objective-c - ios字典数组

ios - NSMutableDictionary 作为数据源 : can´t remove cell