iOS开发专区iOS学习

OC中的深拷贝,浅拷贝,赋值

2017-09-11  本文已影响32人  f2503bba4cfa

深拷贝,浅拷贝概念

深拷贝就是把原来的数据拷贝出来存到一块新的内存中,并有一个新的指针指向新的内存,拷贝结束之后,两个对象虽然存的值是相同的,但是内存地址不一样,两个对象也互不影响,互不干涉。

浅拷贝就是拷贝数据所在内存的地址,让目标对象指针和源对象指向同一片内存空间;在 OC 里面, 使用retain 关键字进行引用计数,就是一种更加保险的浅拷贝。他既让几个指针共用同一片内存空间,又可以在release 由于计数的存在,不会轻易的销毁内存,达到更加简单使用的目的。

OC里面copy与mutable copy与赋值分别是浅拷贝还是深拷贝?

直接写个demo测试下:

@property (nonatomic, strong)NSMutableArray *suyaMArray;

@property (nonatomic, strong)NSArray *suyaIMArray;

self.suyaMArray = @[@1,@2,@3,@4,@6].mutableCopy;

NSMutableArray *suyaAssignmentMArray = self.suyaMArray;

NSMutableArray *suyaMArrayMutableCopy = self.suyaMArray.mutableCopy;

NSArray *suyaMArrayCopy = self.suyaMArray.copy;

NSLog(@"suyaMArray%p,%@",self.suyaMArray,self.suyaMArray);

[self.suyaMArray removeObject:@1];

NSLog(@"suyaMArray%p,%@",self.suyaMArray,self.suyaMArray);

NSLog(@"suyaAssignmentMArray%p,%@",suyaAssignmentMArray,suyaAssignmentMArray);

NSLog(@"suyaMArrayMutableCopy%p,%@",suyaMArrayMutableCopy,suyaMArrayMutableCopy);

NSLog(@"suyaMArrayCopy%p,%@",suyaMArrayCopy,suyaMArrayCopy);

self.suyaIMArray = @[@1,@2,@3,@4,@6].copy;

NSArray *suyaAssignmentIMArray = self.suyaIMArray;

NSArray *suyaIMArrayCopy = self.suyaIMArray.copy;

NSMutableArray *suyaIMArrayMutableCopy = self.suyaIMArray.mutableCopy;

NSLog(@"self.suyaIMArray%p,%@",self.suyaIMArray,self.suyaIMArray);

self.suyaIMArray = @[@2].copy;

NSLog(@"self.suyaIMArray%p,%@",self.suyaIMArray,self.suyaIMArray);

NSLog(@"suyaAssignmentIMArray%p,%@",suyaAssignmentIMArray,suyaAssignmentIMArray);

NSLog(@"suyaIMArrayCopy%p,%@",suyaIMArrayCopy,suyaIMArrayCopy);

NSLog(@"suyaIMArrayMutableCopy%p,%@",suyaIMArrayMutableCopy,suyaIMArrayMutableCopy);

打印结果:

suyaMArray0x6000002450a0,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.089 TestDemo[14783:876388] suyaMArray0x6000002450a0,(

2,

3,

4,

6

)

2017-09-11 17:17:52.089 TestDemo[14783:876388] suyaAssignmentMArray0x6000002450a0,(

2,

3,

4,

6

)

2017-09-11 17:17:52.089 TestDemo[14783:876388] suyaMArrayMutableCopy0x6000002451c0,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.090 TestDemo[14783:876388] suyaMArrayCopy0x6000000727c0,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.090 TestDemo[14783:876388] self.suyaIMArray0x600000072340,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.090 TestDemo[14783:876388] self.suyaIMArray0x600000019920,(

2

)

2017-09-11 17:17:52.090 TestDemo[14783:876388] suyaAssignmentIMArray0x600000072340,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.090 TestDemo[14783:876388] suyaIMArrayCopy0x600000072340,(

1,

2,

3,

4,

6

)

2017-09-11 17:17:52.091 TestDemo[14783:876388] suyaIMArrayMutableCopy0x6000002451f0,(

1,

2,

3,

4,

6

)

先看关于可变对象NSMutableArray *suyaMArray这部分。直接赋值的suyaAssignmentMArray的指针与self.suyaMArray的指针相同且值随着suyaMArray改变而改变,这是浅拷贝;mutablecopy的suyaMArrayMutableCopy的指针与self.suyaMArray的指针不同且值不随着self.suyaMArray改变而改变,这是深拷贝;copy的suyaMArrayCopy与self.suyaMArray的指针不同且值不随着self.suyaMArray改变而改变,这也是深拷贝。

再看关于非可变对象NSArray *suyaIMArray这部分。直接赋值的suyaAssignmentIMArray的指针与self.suyaIMArray的改变前的指针相同,为浅拷贝;copy的suyaIMArrayCopy的指针与self.suyaIMArray的改变前的指针相同,为浅拷贝;mutablecopy的suyaIMArrayMutableCopy的指针与self.suyaIMArray的改变前的指针不同,为深拷贝。

总结如图:

深拷贝浅拷贝

NSString、NSMutableString、NSDictionary、NSMutableDictionary一样适用。表中的对象类型NSMutableArray可以改成可变对象,NSArray可以改为非可变对象。

思考:

如果修饰属性都改为copy那么赋值之后的拷贝类型是什么呢?

上一篇 下一篇

猜你喜欢

热点阅读