美文网首页
copy和mutableCopy

copy和mutableCopy

作者: gpylove | 来源:发表于2018-11-09 16:40 被阅读0次

    概念

    浅拷贝:指针拷贝,不会创建一个新的对象。浅拷贝简单点说就是对内存地址的复制,让目标对象指针和源对象指针指向同一片内存空间。

    深拷贝: 内容拷贝,会创建一个新的对象。深拷贝就是拷贝地址中的内容,让目标对象产生新的内存区域,且将源内存区域中的内容复制到目标内存区域中。

    copy:使用copy生成一个不可变副本,无论是对可变对象拷贝还是对不可变对象拷贝。

    mutableCopy:使用mutableCopy生成一个可变副本,无论是对可变对象拷贝还是对不可变对象拷贝。

    对非集合类对象的copy与mutableCopy:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSString*immutableString =@"origin";

        NSLog(@"%p",immutableString);

        NSString*copyString = [immutableStringcopy];

        NSLog(@"%p",copyString);

        NSMutableString*mutableCopyString = [immutableStringmutableCopy];

        NSLog(@"%p",mutableCopyString);

        NSMutableString *mutableString = [NSMutableString stringWithString:@"origin"];

        NSLog(@"%p",mutableString);

        NSString*aCopyString = [mutableStringcopy];

        NSLog(@"%p",aCopyString);

        NSMutableString*aMutableCopyString = [mutableStringmutableCopy];

        NSLog(@"%p",aMutableCopyString);

    }

    输出结果:

    2018-11-09 16:03:51.223209+0800 OC-Swift[5527:194392] 0x10ab7c068

    2018-11-09 16:03:51.223402+0800 OC-Swift[5527:194392] 0x10ab7c068

    2018-11-09 16:03:51.223547+0800 OC-Swift[5527:194392] 0x600000b679f0

    2018-11-09 16:03:51.223668+0800 OC-Swift[5527:194392] 0x600000b61440

    2018-11-09 16:03:51.223760+0800 OC-Swift[5527:194392] 0xf995fba58d33f6fa

    2018-11-09 16:03:51.223864+0800 OC-Swift[5527:194392] 0x600000b61650

            可见, 在非集合类对象中,对 immutable(不可变) 对象进行 copy 操作,是指针复制,mutableCopy 操作时内容复制;对 mutable(可变) 对象进行 copy 和 mutableCopy 都是内容复制

    对集合类对象的copy与mutableCopy:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSArray*immutableArray =  @[@"a",@"b",@"c",@"d"];

        NSLog(@"%p",immutableArray);

        NSString*copyArray = [immutableArraycopy];

        NSLog(@"%p",copyArray);

        NSMutableString*mutableCopyArray = [immutableArraymutableCopy];

        NSLog(@"%p",mutableCopyArray);

        NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:[NSMutableArray arrayWithObject:@"abcd"]];

        NSLog(@"%p",mutableArray);

        NSArray*aCopyArray = [mutableArraycopy];

        NSLog(@"%p",aCopyArray);

        NSMutableArray*aMutableCopyArray = [mutableArraymutableCopy];

        NSLog(@"%p",aMutableCopyArray);

    }

    输出结果:

    2018-11-09 16:13:13.706506+0800 OC-Swift[5638:198595] 0x600001a3c630

    2018-11-09 16:13:15.574032+0800 OC-Swift[5638:198595] 0x600001a3c630

    2018-11-09 16:13:16.775059+0800 OC-Swift[5638:198595] 0x600001a3c6f0

    2018-11-09 16:13:18.946782+0800 OC-Swift[5638:198595] 0x600001a30ea0

    2018-11-09 16:13:23.310313+0800 OC-Swift[5638:198595] 0x60000167afc0

    2018-11-09 16:13:25.532082+0800 OC-Swift[5638:198595] 0x600001a3c690

            在集合类对象中,对 immutable (不可变)对象进行 copy,是指针复制, mutableCopy 是内容复制;对 mutable(可变) 对象进行 copy 和 mutableCopy 都是内容复制。

    但是:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:[NSMutableArray arrayWithObject:@"abcd"]];

        NSArray*aCopyArray = [mutableArraycopy];

        NSMutableArray*aMutableCopyArray = [mutableArraymutableCopy];

        NSMutableArray*firstArray = mutableArray.firstObject;

        [firstArrayaddObject:@"efg"];

        NSLog(@"mutableArray = %@\n aCopyArray = %@\n aMutableCopyArray = %@\n",mutableArray,aCopyArray,aMutableCopyArray);

    }

    输出结果:

    2018-11-09 16:19:32.247795+0800 OC-Swift[5700:201553] mutableArray = (

            (

            abcd, efg

        )

    )

     aCopyArray = (

            (

            abcd,  efg

        )

    )

     aMutableCopyArray = (

            (

            abcd, efg

        )

    )

    可见,集合对象的内容复制仅限于对象本身,对象元素仍然是指针复制。就是:

    [immutableObject copy]// 浅复制

    [immutableObject mutableCopy]//单层深复制

     [mutableObject copy]//单层深复制

    [mutableObject mutableCopy]//单层深复制

    后记:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view, typically from a nib.

        NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:[NSMutableArray arrayWithObject:@"abcd"]];

        NSMutableArray*aMutableCopyArray = [[NSMutableArray alloc] initWithArray:mutableArray copyItems:YES];

        NSMutableArray*firstArray = mutableArray.firstObject;

        [firstArray addObject:@"efg"];

        NSLog(@"mutableArray = %@\n aMutableCopyArray = %@\n",mutableArray,aMutableCopyArray);

    }

    输出结果:

    2018-11-09 16:36:23.086028+0800 OC-Swift[5879:208849] mutableArray = (

            (

            abcd, efg

        )

    )

     aMutableCopyArray = (

            (

            abcd

        )

    )

            可以用NSArray的这个- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag方法进行深层次的复制。

            NSDictionary的这个- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary copyItems:(BOOL)flag方法进行深层次的复制。

            NSSet的这个- (instancetype)initWithSet:(NSSet *)set copyItems:(BOOL)flag方法进行深层次的复制。

    相关文章

      网友评论

          本文标题:copy和mutableCopy

          本文链接:https://www.haomeiwen.com/subject/hcsnxqtx.html