据说单独的NSArray被编辑副本污染 [英] Supposedly separate NSArray being contaminated by editing a copy

查看:97
本文介绍了据说单独的NSArray被编辑副本污染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableView中有一些文本域,的值表示文本域都链接到特定的位置在一个二维数组(NSMutableArrays的NSArray的)。

I have a TableView with some TextFields in. The values of said TextFields are linked to certain positions in a 2D Array (NSArray of NSMutableArrays).

这是最初的清洁数组的定义是这样:

An initial clean array is defined as so:

self.cleanEditContents = @[
                      [@[@-1,@-1] mutableCopy],
                      [@[@0,@80] mutableCopy],
                      [@[@0,@500] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy],
                      [@[@-1,@-1] mutableCopy]
                      ];

此阵应该是分开从主动阵列,使得有源阵列可通过一个按钮preSS复位。

This array is supposed to be kept separate from the 'active' array, so that the active array can be reset on a button press.

我用 self.editContents = [self.cleanEditContents复制] 来设置有源阵列包括直接清洗后阵列填充和按钮上preSS

I use self.editContents = [self.cleanEditContents copy]; to set the active array both directly after the clean array is populated and on a button press.

有就是,即使我重置阵列并调用reloadData和setNeedsLayout(矫枉过正?可能),数字不重的问题。我试图输出相同位置的值在两个数组和事实证明,有源阵列所做的任何更改污染干净阵列。

There is a problem where even though I reset the array and call reloadData and setNeedsLayout (overkill? probably), the numbers don't reset. I tried outputting the values of the same position in both arrays and it turns out that any changes made to the active array contaminate the clean array.

推荐答案

复制不浅拷贝。换句话说, self.editContents self.cleanEditContents 均引用同一​​套可变阵列。所以,如果你在一个更新一个可变数组,变化是出现在其他。

copy does a shallow copy. In other words, self.editContents and self.cleanEditContents both reference the same set of mutable arrays. So if you update a mutable array in one, the change is seen in the other.

要创建 self.editContents ,创建与内阵列的可变副本的新数组。

To create self.editContents, create a new array with mutable copies of the inner arrays.

NSMutableArray *tmp = [NSMutableArray array];
for (NSArray *array in self.cleanEditContents) {
    [tmp addObject:[array mutableCopy]];
}
self.editContents = tmp;

这篇关于据说单独的NSArray被编辑副本污染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆