深拷贝和浅拷贝苹果官网上的解释如下:
There are two kinds of object copying: shallow copies and deep copies. The normal copy is a shallow copy that produces a new collection that shares ownership of the objects with the original. Deep copies create new objects from the originals and add those to the new collection.
In the case of these objects, a shallow copy means that a new collection object is created, but the contents of the original collection are not duplicated—only the object references are copied to the new container.
A deep copy duplicates the compound object as well as the contents of all of its contained objects.
浅拷贝不会创建新的对方和原有对象共享同一个对象,深拷贝会将原有对象进行赋值,生成完全独立的对象.
Copy 与 MutableCopy
iOS 深拷贝和浅拷贝可以通过Copy和MutableCopy来阐述,先来看两段网上的经典代码:
<pre><code>`- (void)copyTest {
NSString *string = @"FlyElephant";
NSString *copyString = [string copy];
NSMutableString *mutableCopyString = [string mutableCopy];
[mutableCopyString appendString:@"test"];
NSLog(@"NSString原始指针:%p copy之后的指针 = %p mutableCopy之后的指针 = %p", string, copyString, mutableCopyString);
}
-
(void)mutaleCopyTest {
NSMutableString *string = [[NSMutableString alloc] initWithString:@"FlyELephant"];
NSMutableString *copyString = [string copy];
NSMutableString *mutableCopyString = [string mutableCopy];NSLog(@"NSMutableString原始指针:%p copy之后的指针 = %p mutableCopy之后的指针 = %p", string, copyString, mutableCopyString);
[mutableCopyString appendString:@"test"];
NSLog(@"原始字符串:%@---copy之后的字符串:%@---mutalbeCopy之后的字符串:%@",string,copyString,mutableCopyString);
}`</code></pre>
通过以上例子我们似乎可以得出以下结论:
不可变对象: 不可变对象执行Copy的时候是指针赋值属于浅拷贝,MutableCopy是内容复制属于深拷贝.
可变对象: 可变对象在执行Copy和MutableCopy的时候都是内容复制属于深拷贝.
再来看一段代码:
<pre><code>` NSMutableString *str1 = [NSMutableString stringWithString:@"FlyElephant"];
NSMutableString *str2 = [NSMutableString stringWithString:@"Keso"];
NSMutableString *str3 = [NSMutableString stringWithString:@"Objective-C"];
NSMutableString *str4 = [NSMutableString stringWithString:@"Swift"];
NSLog(@"str1:%p str2:%p str3:%p str4:%p", str1,str2,str3,str4);
NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
str1,
str2,
str3,
str4,
nil
];
NSMutableArray *dataArray2 = [dataArray mutableCopy];
NSMutableString *mStr;
mStr = dataArray[0];
[mStr appendString:@"--append"];
NSLog(@"dataArray:%@",dataArray);
NSLog(@"dataArray2:%@",dataArray2);
[dataArray addObject:@"test"];
NSLog(@"dataArray:%@",dataArray);
NSLog(@"dataArray2:%@",dataArray2);`</code></pre>
可变数组经过mutableCopy之后如果深拷贝,那么作为独立的对象,数组之间应该是没有影响的.
再来看一段代码:
<pre><code>` NSMutableString *str1 = [NSMutableString stringWithString:@"FlyElephant"];
NSMutableString *str2 = [NSMutableString stringWithString:@"Keso"];
NSMutableString *str3 = [NSMutableString stringWithString:@"Objective-C"];
NSMutableString *str4 = [NSMutableString stringWithString:@"Swift"];
NSLog(@"str1:%p str2:%p str3:%p str4:%p", str1,str2,str3,str4);
NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:
str1,
str2,
str3,
str4,
nil
];
NSMutableArray *dataArray2 = [dataArray mutableCopy];
NSMutableString *mStr;
mStr = dataArray[0];
[mStr appendString:@"--append"];
NSLog(@"dataArray:%@",dataArray);
NSLog(@"dataArray2:%@",dataArray2);
[dataArray addObject:@"test"];
NSLog(@"dataArray:%@",dataArray);
NSLog(@"dataArray2:%@",dataArray2);`</code></pre>
copy: 不可变对象执行Copy之后不会生成新的对象,对象会共享,可变对象执行Copy之后生成新的对象,而且新的对象不可变.属于浅拷贝.
mutableCopy: 不可变对象和可变对象执行mutableCopy之后生成新的对象,复制内容到新的内存中,但是属于浅拷贝.
属性 与 自定义对象
iOS属性中字符串使用copy比较频繁,定义两个属性字符串,来对比一下与strong的区别:
<pre><code>`@property (copy, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *infoDetail;`</code></pre>
测试代码:
<pre><code>` NSMutableString *test = [[NSMutableString alloc] initWithString:@"FlyElephant"];
self.name = test;
self.infoDetail = test;
NSLog(@"name:%@----description:%@",self.name,self.infoDetail);
[test appendString:@"test"];
NSLog(@"name:%@----description:%@",self.name,self.infoDetail);`</code></pre>
属性如果是字符串建议使用copy属性,避免如果是可变复制,属性发生变化.
自定义对象如果要实现拷贝,需要实现NSCopying协议:
<pre><code>`@interface Course()<NSCopying>
@end
@implementation Course
- (id)copyWithZone:(NSZone *)zone {
Course *course = [[Course allocWithZone:zone] init];
course.courseName = [self.courseName mutableCopy];
return course;
}
@end`</code></pre>
自定义对象拷贝:
<pre><code>` Course *course = [[Course alloc] init];
course.courseName = @"iOS 学习开发";
Course *copyCourse = [course copy];
NSLog(@"Course:%@----课程名字:%p",course,course.courseName);
NSLog(@"CopyCourse: %@----课程名字:%p",copyCourse,copyCourse.courseName);`</code></pre>
自定义对象拷贝,要记得属性的拷贝.