copy 与 mutableCopy

2017-05-24  本文已影响23人  JC_Wang

1、主要区别

1、copy拷贝出来的对象类型总是不可变类型(例如, NSString, NSDictionary, NSArray等等)

2、mutableCopy拷贝出来的对象类型总是可变类型(例如, NSMutableString, NSMutableDictionary, NSMutableArray等等)

image_1bgt350gvp4pova1hpnfr1cg9.png-26.2kBimage_1bgt350gvp4pova1hpnfr1cg9.png-26.2kB

2、浅拷贝和深拷贝

一、浅拷贝的地址 copy

        NSArray *arrA = @[@"one",@"two"];
        NSArray *arrB = [arrA copy];
        //此时共享一块内存地址,指针引用
        NSLog(@"\nA %@ \t内存地址是%p \nB %@ \t内存地址是%p",arrA,arrB,arrA,arrB);
image_1bgt3pfdh97nle12c2im1am8m.png-29kBimage_1bgt3pfdh97nle12c2im1am8m.png-29kB

二、深拷贝的内容 mutableCopy

        NSMutableArray *arrC = [@[@"one",@"two",@"three"] mutableCopy];
        ///
        NSArray *arrD = [arrC mutableCopy];
        ///
        //此时不同内存地址
        NSLog(@"--- 赋值 --- \nC %@ \t内存地址是%p \nD %@ \t内存地址是%p",arrC,arrC,arrD,arrD);
        //移除最后一个元素后
        [arrC removeLastObject];
        NSLog(@"--- 移除 --- \nC %@ \t内存地址是%p \nD %@ \t内存地址是%p",arrC,arrC,arrD,arrD);
image_1bgt4672d5gm1fpn10131ld43h613.png-59.3kBimage_1bgt4672d5gm1fpn10131ld43h613.png-59.3kB

三、copy一个可变的对象

        NSMutableArray *arrC = [@[@"one",@"two",@"three"] mutableCopy];
        ///
        NSArray *arrD = [arrC copy];
        ///
        //此时不同内存地址
        NSLog(@"--- 赋值 --- \nC %@ \t内存地址是%p \nD %@ \t内存地址是%p",arrC,arrC,arrD,arrD);
        //移除最后一个元素后
        [arrC removeLastObject];
        NSLog(@"--- 移除 --- \nC %@ \t内存地址是%p \nD %@ \t内存地址是%p",arrC,arrC,arrD,arrD);
image_1bgt4dqo71cc3178j9rec3r10dj1g.png-59.3kBimage_1bgt4dqo71cc3178j9rec3r10dj1g.png-59.3kB

二和三的区别只在第3行,但是结果都是深拷贝,所以总结就是对可变的对象 copy 或者 mutableCopy,都是深拷贝内容

3、深拷贝遗留的问题

show the code

        NSMutableArray *arr_mut_dog = [NSMutableArray array];

        Dog *dog1 = [[Dog alloc] init];
        dog1.name = @"花gou";
        Dog *dog2 = [[Dog alloc] init];
        dog2.name = @"xiao黄";
        //添加到可变数组
        [arr_mut_dog addObject:dog1];
        [arr_mut_dog addObject:dog2];

        NSArray *newArr = [arr_mut_dog mutableCopy];

        NSLog(@"-- 先 --\narr_mut_dog %@ \t内存地址是%p \nnewArr %@ \t内存地址是%p",arr_mut_dog,arr_mut_dog,newArr,newArr);

        //移除一个
        [arr_mut_dog removeLastObject];
        NSLog(@"-- 后 --\narr_mut_dog %@ \t内存地址是%p \nnewArr %@ \t内存地址是%p",arr_mut_dog,arr_mut_dog,newArr,newArr);

控制台输出


image_1bgt59q1j10143ql1tu61pdo13t81t.png-77kBimage_1bgt59q1j10143ql1tu61pdo13t81t.png-77kB

4、实现完全深拷贝

作为元素的类,实现NSCopying,NSMutableCopying协议

------ dog.h --------
#import <Foundation/Foundation.h>
@interface Dog : NSObject<NSCopying,NSMutableCopying>
@property (nonatomic, copy) NSString *name;
@end
------ dog.m --------
#import "Dog.h"
@implementation Dog
- (id)copyWithZone:(NSZone *)zone
{
    Dog *dog = [[Dog allocWithZone:zone] init];
    dog.name = _name;
    return dog;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
    Dog *dog = [[Dog allocWithZone:zone] init];
    dog.name = _name;
    return dog;
}
--- main ----
        NSMutableArray *arr_mut_dog = [NSMutableArray array];
        Dog *dog1 = [[Dog alloc] init];
        dog1.name = @"花gou";
        Dog *dog2 = [[Dog alloc] init];
        dog2.name = @"xiao黄";
        //添加到可变数组
        [arr_mut_dog addObject:dog1];
        [arr_mut_dog addObject:dog2];
        NSArray *newArr = [arr_mut_dog copy];
        NSLog(@"-- 先 --\narr_mut_dog %@ \t内存地址是%p \nnewArr %@ \t内存地址是%p",arr_mut_dog,arr_mut_dog,newArr,newArr);
        //移除一个
        [arr_mut_dog removeLastObject];
        NSLog(@"-- 后 --\narr_mut_dog %@ \t内存地址是%p \nnewArr %@ \t内存地址是%p",arr_mut_dog,arr_mut_dog,newArr,newArr);
@end
image_1bgt64d8e95k1vln1j97na2179r2a.png-78.6kBimage_1bgt64d8e95k1vln1j97na2179r2a.png-78.6kB
上一篇下一篇

猜你喜欢

热点阅读