iOS-深拷贝与浅拷贝

深拷贝和浅拷贝苹果官网上的解释如下:

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>

FlyElephant.png

通过以上例子我们似乎可以得出以下结论:
不可变对象: 不可变对象执行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>
FlyElephant.png

可变数组经过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>
FlyElephant.png

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>
FlyElephant.png

属性如果是字符串建议使用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>

自定义对象拷贝,要记得属性的拷贝.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、对象拷贝有两种方式:浅复制和深复制。顾名思义,浅复制,并不拷贝对象本身,仅仅是拷贝指向对象的指针;深复制是直接...
    滴答大阅读 785评论 0 2
  • 浅拷贝:拷贝容易本身,返回一个对象,指向相同的内存地址. 深层复制:拷贝容器本身,返回一个对象,指向不同的内存地址...
    CoderShmily阅读 7,049评论 5 9
  • 大家好,我是西瓜,现居广州。在今年想要回顾梳理一下OC的相关知识点。今天就先从基础但不简单的深拷贝与浅拷贝开始吧。...
    watermelon_lp阅读 421评论 0 5
  • 道歉 之前我的一篇关于深浅拷贝的文章,里面有诸多错误,主要是混淆了混淆copy、mutableCopy和深浅拷贝,...
    西木柚子阅读 3,204评论 20 46
  • 概念 在Objective-C中并不是所有的对象都支持Copy,MutableCopy,遵守NSCopying协议...
    LeoAu阅读 8,865评论 10 28