NSMutableArray更改为副本,从而导致父NSMutableArray中的更改 [英] NSMutableArray changes to the copy causing changes in the parent NSMutableArray as well
问题描述
我有一个NSMutableArray也是我在我的一个视图控制器中使用的委托。
I have a NSMutableArray is my delegate that I am using in one of my view controllers as well.
所以在viewDidLoad中我制作了一个我的NSMutableArray的可变副本这个
So in viewDidLoad I make a mutable copy of my NSMutableArray like this
@implementation ItemsViewController{
AppDelegate *mydelegate;
NSMutableArray* allItems;
}
在viewDidLoad中
In viewDidLoad
allItems = [mydelegate.array mutableCopy];
现在我在allItems中做出的任何变化MutableArray也会导致我的mydelegate.array发生变化。我做错了什么?
Now whatever changes I make in my allItems MutableArray also cause changes in my mydelegate.array. Am I doing something wrong?
此外,我在委托中的数组定义如下
Also my array in the delegate is defined as follows
@property (nonatomic, strong) NSMutableArray *array;
推荐答案
你应该做一个deepCopy,我用这一个并且完美运行,由 Sherm Pendley†。
You should do a deepCopy, i use this one and works perfectly, made by Sherm Pendley †.
这篇关于NSMutableArray更改为副本,从而导致父NSMutableArray中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!