2.iOS“深拷贝“和“浅拷贝“

2020-02-22  本文已影响0人  马小撂

在iOS系统中,并不是所有的
对象都支持copyMutableCopy的,必须的遵循NSCopying实现copyWithZone或者遵循NSMutableCopying实现mutableCopyWithZone方法,否者调用copyMutableCopy就会报系统异常。

一般我们从对象容器两个概念上进行区分
1)非容器类不可变对象:NSString
2)非容器类可变对象:NSMutableString
3)容器类不可变对象:NSArray
4)容器类可变对象:NSMutableArray

    NSString *origStr = @"非容器类不可变对象";
    NSString *copyStr = [origStr copy];
    NSString *mutaStr = [origStr mutableCopy];
    
    NSLog(@"origStr : %p, class: %@", origStr, [origStr class]);
    NSLog(@"copyStr : %p, class: %@", copyStr, [copyStr class]);
    NSLog(@"mutaStr : %p, class: %@", mutaStr, [mutaStr class]);

输出:

origStr : 0x108877068, class: __NSCFConstantString
copyStr : 0x108877068, class: __NSCFConstantString
mutaStr : 0x600001992d90, class: __NSCFString

1.对于非容器类不可变对象来讲,copy为浅拷贝,MutableCopy为深拷贝。
2.浅拷贝的指针和原对象的一致,返回的是不可变对象。
3.深拷贝返回新的内存地址,并返回一个可变对象。

    NSMutableString *origStr = [NSMutableString stringWithString:@"非容器类可变对象"];
    NSString *copyStr = [origStr copy];
    NSString *mutaStr = [origStr mutableCopy];
    
    NSLog(@"origStr : %p, class: %@", origStr, [origStr class]);
    NSLog(@"copyStr : %p, class: %@", copyStr, [copyStr class]);
    NSLog(@"mutaStr : %p, class: %@", mutaStr, [mutaStr class]);

输出:

origStr : 0x600001530cc0, class: __NSCFString
copyStr : 0x600001530cf0, class: __NSCFString
mutaStr : 0x600001530c60, class: __NSCFString

对于非容器类可变对象来讲,copyMutableCopy都为深拷贝。会开辟新的内存地址,并返回一个可变对象。

    NSMutableString *mutableStr = [NSMutableString stringWithString:@"容器类可变对象"];
    NSArray *origArr = @[mutableStr,@"容器类可变对象"];
    NSArray *copyArr = [origArr copy];
    NSArray *mutaArr = [origArr mutableCopy];
    
    NSLog(@"origArr : %p, class: %@", origArr, [origArr class]);
    NSLog(@"copyArr : %p, class: %@", copyArr, [copyArr class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr, [mutaArr class]);
    
    
    NSLog(@"======原对象=====");
    NSLog(@"origArr : %p, class: %@", origArr[0], [origArr[0] class]);
    NSLog(@"origArr : %p, class: %@", origArr[1], [origArr[1] class]);
   
    
    NSLog(@"======copy对象=====");
    NSLog(@"copyArr : %p, class: %@", copyArr[0], [copyArr[0] class]);
    NSLog(@"copyArr : %p, class: %@", copyArr[1], [copyArr[1] class]);
    
    NSLog(@"======mutableCopy对象=====");
    NSLog(@"mutaArr : %p, class: %@", mutaArr[0], [mutaArr[0] class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr[1], [mutaArr[1] class]);

输出

origArr : 0x6000012c3b80, class: __NSArrayI
copyArr : 0x6000012c3b80, class: __NSArrayI
mutaArr : 0x600001cfadf0, class: __NSArrayM
======原对象=====
origArr : 0x600001cfaf70, class: __NSCFString
origArr : 0x105013070, class: __NSCFConstantString
======copy对象=====
copyArr : 0x600001cfaf70, class: __NSCFString
copyArr : 0x105013070, class: __NSCFConstantString
======mutableCopy对象=====
copyArr : 0x600001cfaf70, class: __NSCFString
copyArr : 0x105013070, class: __NSCFConstantString

从输出可以看出容器类不可变对象mutableCopy返回了一个新的容器,但容器中的对象仍然是同一份。称之为不完全拷贝。

    NSMutableString *mutableStr = [NSMutableString stringWithString:@"容器类可变对象"];
    NSMutableArray *origArr = [NSMutableArray arrayWithArray:@[mutableStr,@"容器类可变对象"]];
    NSMutableArray *copyArr = [origArr copy];
    NSMutableArray *mutaArr = [origArr mutableCopy];
    
    NSLog(@"origArr : %p, class: %@", origArr, [origArr class]);
    NSLog(@"copyArr : %p, class: %@", copyArr, [copyArr class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr, [mutaArr class]);
    
    
    NSLog(@"======原对象=====");
    NSLog(@"origArr : %p, class: %@", origArr[0], [origArr[0] class]);
    NSLog(@"origArr : %p, class: %@", origArr[1], [origArr[1] class]);
   
    
    NSLog(@"======copy对象=====");
    NSLog(@"copyArr : %p, class: %@", copyArr[0], [copyArr[0] class]);
    NSLog(@"copyArr : %p, class: %@", copyArr[1], [copyArr[1] class]);
    
    NSLog(@"======mutableCopy对象=====");
    NSLog(@"mutaArr : %p, class: %@", mutaArr[0], [mutaArr[0] class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr[1], [mutaArr[1] class]);

输出:

origArr : 0x600002c49770, class: __NSArrayM
copyArr : 0x600002264220, class: __NSArrayI
mutaArr : 0x600002c499b0, class: __NSArrayM
 ======原对象=====
origArr : 0x600002c49a10, class: __NSCFString
origArr : 0x1026be070, class: __NSCFConstantString
======copy对象=====
copyArr : 0x600002c49a10, class: __NSCFString
copyArr : 0x1026be070, class: __NSCFConstantString
======mutableCopy对象=====
mutaArr : 0x600002c49a10, class: __NSCFString
mutaArr : 0x1026be070, class: __NSCFConstantString

由输出可以看出对于容器类可变对像来说mutableCopycopy都返回一个新的容器,但容器中的对象仍然是同一份。为不完全拷贝。

    NSMutableString *mutableStr = [NSMutableString stringWithString:@"容器类可变对象"];
    NSMutableArray *origArr = [NSMutableArray arrayWithArray:@[mutableStr,@"容器类可变对象"]];
    NSMutableArray *mutaArr = [[NSMutableArray alloc] initWithArray:origArr copyItems:YES];
    
    NSLog(@"origArr : %p, class: %@", origArr, [origArr class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr, [mutaArr class]);
    
    NSLog(@"======原对象=====");
    NSLog(@"origArr : %p, class: %@", origArr[0], [origArr[0] class]);
    NSLog(@"origArr : %p, class: %@", origArr[1], [origArr[1] class]);
    
    NSLog(@"======mutableCopy对象=====");
    NSLog(@"mutaArr : %p, class: %@", mutaArr[0], [mutaArr[0] class]);
    NSLog(@"mutaArr : %p, class: %@", mutaArr[1], [mutaArr[1] class]);

输出:

origArr : 0x6000009bb1e0, class: __NSArrayM
mutaArr : 0x6000009bafa0, class: __NSArrayM
======原对象=====
origArr : 0x6000009bb000, class: __NSCFString
origArr : 0x10c358070, class: __NSCFConstantString
======mutableCopy对象=====
mutaArr : 0x6000009baac0, class: __NSCFString
mutaArr : 0x10c358070, class: __NSCFConstantString

由输出可以看出 通过这个 copyItems 可变容器的可变对像内存地址不同,实现了完全深拷贝。

总结:

1.copy对于不可变对象来说就是浅拷贝,对于可变对象来说就是深拷贝。
2.mutableCopy:始终是深拷贝,但是不完全拷贝,完全深拷贝需要自己实现。

自定义类对象的深浅拷贝

在OC中不是所有的类都支持拷贝,只有遵循<NSCopying>才支持copy,只有遵循<NSMutableCopying>才支持mutableCopy。如果没有遵循,拷贝时会直接Crash
例:创建一个继承于NSObject的类Person,直接调用copy或者mutableCopy

Person *person = [[Person alloc] init];
[person copy];
[person mutableCopy];

崩溃输出:

unrecognized selector sent to instance 0x6000031f4170

遵循协议,实现方法

#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCopying, NSMutableCopying>
@property (nonatomic, copy) NSString *name;
@end


#import "Person.h"
@implementation Person
- (id)copyWithZone:(nullable NSZone *)zone{
    Person *person = [Person allocWithZone:zone];
    person.name = self.name;
    return person;
}
- (id)mutableCopyWithZone:(nullable NSZone *)zone{
    Person *person = [Person allocWithZone:zone];
    person.name = self.name;
    return person;
}
@end
上一篇 下一篇

猜你喜欢

热点阅读