旧数组中所做的更改也反映在新复制的数组中

时间:2015-01-05 08:26:27

标签: objective-c cocoa

使用mutableCopy将数组的内容复制到另一个数组,但如果在原始数组中进行了更改,它也会在新数组中显示更改。下面是使用的代码。

NSArray *newArr = [mainArray mutableCopy];
[[newArr objectAtIndex:indexpath.section] replaceObjectAtIndex:indexpath.row withObject:@""];
[[mainArray objectAtIndex:indexpath.section] removeObjectAtIndex:indexpath.row];
objAppDelegate.arrayProfile = [newArr objectAtIndex:2];

我必须从原始数组mainArray中删除值,但不想反映新数组中的更改。请指导。

2 个答案:

答案 0 :(得分:0)

对于NSArray,您必须使用copy not mutablecopy

 NSArray *newArr = [mainArray copy];

答案 1 :(得分:-1)

您需要复制主阵列的所有项目

NSMutableArray *newArr = [[NSMutableArray alloc] initWithCapacity:mainArray.count];
for (NSObject* item in mainArray) {
    [newArr addObject:[item copy]];
}