将对象从一个NSMutableArray复制到另一个NSMutableArray(copy objects from one NSMutableArray to another NSMutableArray)
我试图理解从一个NSMutableArray复制对象到另一个NSMutableArray。 请考虑以下两种情况:1 - 将原始文件复制到克隆,其中克隆中的更改将影响原始文件。 2 - 将原件复制到克隆,其中关闭的更改不会影响原件。
首先,我尝试使用以下代码首先生成场景#1。 根据我的理解,当复制数组不使用'mutablecopy'时,克隆数组将只保存指向原始字符串对象的指针。 所以,如果我要将克隆的第一个元素更改为另一个对象,原始的第一个元素会改变得太对吗? ......但这不是我得到的结果。 为什么?
事实上,当我使用mutablecopy时
[self.cloneArray addObject:[[self.originalArray objectAtIndex:i] mutableCopy]];
我得到了相同的结果。 我很困惑。
ArrayClass.h
@interface ArrayClass : NSObject { NSMutableArray *_originalArray; NSMutableArray *_cloneArray; } @property (nonatomic, retain) NSMutableArray *originalArray; @property (nonatomic, retain) NSMutableArray *cloneArray;
ArrayClass.m
@synthesize originalArray = _originalArray; @synthesize cloneArray = _cloneArray; _originalArray = [[NSMutableArray alloc] initWithObjects: @"one", @"two", @"three", @"four", @"five", nil]; _cloneArray = [[NSMutableArray alloc] initWithCapacity:[self.originalArray count]]; for (int i=0; i<5; i++) { [self.cloneArray addObject:[self.originalArray objectAtIndex:i]]; } // make change to the first element of the clone array [self.cloneArray replaceObjectAtIndex:0 withObject:@"blah"]; for (int n=0; n<5; n++) { NSLog(@"Original:%@ --- Clone:%@", [self.originalArray objectAtIndex:n], [self.cloneArray objectAtIndex:n]); }
...
2011-03-27 03:23:16.637 StringTest[1751:207] Original:one --- Clone:blah 2011-03-27 03:23:16.638 StringTest[1751:207] Original:two --- Clone:two 2011-03-27 03:23:16.639 StringTest[1751:207] Original:three --- Clone:three 2011-03-27 03:23:16.642 StringTest[1751:207] Original:four --- Clone:four 2011-03-27 03:23:16.643 StringTest[1751:207] Original:five --- Clone:five
I am trying to understand copying objects from one NSMutableArray to another. Consider the following 2 scenarios: 1 - copying original to clone where changes in the clone will affect the original. 2 - copying original to clone where the changes in the close will NOT affect the original.
First, I am trying to produce scenario #1 first with the following code. From what I understand, when copying array not using 'mutablecopy', the clone array will just hold the pointer to the same string objects in the original. So if I were to change the first element of the clone to a different object, the first element of the original would change too right? ... but that's not the result I am getting. Why?
Matter of fact, when I use mutablecopy
[self.cloneArray addObject:[[self.originalArray objectAtIndex:i] mutableCopy]];
I get the same result. I am confused.
ArrayClass.h
@interface ArrayClass : NSObject { NSMutableArray *_originalArray; NSMutableArray *_cloneArray; } @property (nonatomic, retain) NSMutableArray *originalArray; @property (nonatomic, retain) NSMutableArray *cloneArray;
ArrayClass.m
@synthesize originalArray = _originalArray; @synthesize cloneArray = _cloneArray; _originalArray = [[NSMutableArray alloc] initWithObjects: @"one", @"two", @"three", @"four", @"five", nil]; _cloneArray = [[NSMutableArray alloc] initWithCapacity:[self.originalArray count]]; for (int i=0; i<5; i++) { [self.cloneArray addObject:[self.originalArray objectAtIndex:i]]; } // make change to the first element of the clone array [self.cloneArray replaceObjectAtIndex:0 withObject:@"blah"]; for (int n=0; n<5; n++) { NSLog(@"Original:%@ --- Clone:%@", [self.originalArray objectAtIndex:n], [self.cloneArray objectAtIndex:n]); }
...
2011-03-27 03:23:16.637 StringTest[1751:207] Original:one --- Clone:blah 2011-03-27 03:23:16.638 StringTest[1751:207] Original:two --- Clone:two 2011-03-27 03:23:16.639 StringTest[1751:207] Original:three --- Clone:three 2011-03-27 03:23:16.642 StringTest[1751:207] Original:four --- Clone:four 2011-03-27 03:23:16.643 StringTest[1751:207] Original:five --- Clone:five
原文:https://stackoverflow.com/questions/5447853
满意答案
默认情况下,
object prototype
是一个空对象。classA.prototype = { x: 4, y: 6 }; classA.prototype.prototype = { z: 10 };
相当于
classA.prototype = { x: 4, y: 6, prototype: { z: 10 }};
您只需将名为
prototype
的属性添加到classA
z
是属于classA
的对象prototype
的属性
alert(foo.prototype.z);
将工作By default an
object prototype
is an empty object.classA.prototype = { x: 4, y: 6 }; classA.prototype.prototype = { z: 10 };
is equivalent to
classA.prototype = { x: 4, y: 6, prototype: { z: 10 }};
you just added a property named
prototype
toclassA
z
is a property of the objectprototype
belonging toclassA
alert(foo.prototype.z);
will work
相关问答
更多JavaScript初学者:为什么这不起作用?(JavaScript beginner: why does this not work?)
javascript原型:为什么这不起作用?(javascript prototyping: why doesn't this work?)
Javascript中基本原型的问题(Problems with basic prototyping in Javascript)
Javascript OOP - 继承和原型设计(Javascript OOP - inheritance and prototyping)
为什么这不起作用?(Why doesn't this work?)
使用Javascript进行原型设计(Prototyping in Javascript)
JavaScript / jQuery为什么这不起作用?(JavaScript / jQuery why doesn't this work?)
Javascript:使用性能参数对函数进行原型设计(Javascript: Prototyping a function with arguments for performance)
为什么这不起作用?(Why doesn't this work? sqlit3 php)
Javascript中的单例和原型(Singleton and Prototyping in Javascript)
相关文章
更多Guava Objects类详解
微信,从一个QQ到另一个QQ
将某个条目复制到其他分组的方案
Hadoop 本地文件复制到hdfs目录
关于对象复制时出现内容不一致的问题
原创:用VBA实现将鼠标选择的单元格按照指定格式合并并复制到剪切板
数据复制问题
快速了解mybatis
对象关系映射(ORM)-Oceanus编码调用
一段代码的疑问
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!