我有下面的代码...
NSMutableArray *tArray = [[NSMutableArray alloc] init];
tArray = mainArray;
[tArray removeObjectAtIndex:mainArray.count-1];
在 mainArray 中我有 4 个项目。
当我运行上面的代码时,我的预期如下。
mainArray >> 4 items
tArray >> 3 items
但是我得到的结果如下。
mainArray >> 3 items (this is WRONG)
tArray >> 3 items
知道为什么当我从主数组中删除对象时对象从主数组中删除。
最佳答案
tArray 和 mainArray 是指针。在您的情况下,它们指的是同一个数组。你应该使用
NSMutableArray *tArray = [mainArray mutableCopy];
真正复制数组。
[[NSMutableArray alloc] init]
不需要,因为结果将被丢弃。
关于ios - removeObjectAtIndex : is removing data from the main array and duplicate array as well,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23077283/