Property copy

概念

    Property修饰关键字copy

     创建一个引用计数为1的一个对象,释放旧对象,然后赋值,此属性只对那些实行了NSCopying协议的对象类型有效。copy方法默认是复制该对象的不可变副本。

    copy的setrer方法

/** 第一种setter方法  ARC  模式下

@param Name 输入值 */

- (void)setName:(NSString *)Name{

    if(_Name != Name){

        _Name = nil;

        _Name = [Name copy];

    }

}

/** 第二种setter方法

@param Name 输入值 */

- (void)setName:(NSString *)Name{

        _Name = [Name copy];

}

    上述两种方法,根据概念实现setter方法的时候选择第一个对象是合适的,判断这个对象的值是否是该属性变量 的值,不是,首先让旧属性的变量置nil,ARC模式下,会直接将这个变量清空,释放,然后在复制一份输入值给这个属性的变量。

    然后根据我写过的深、浅拷贝来进行分析。

    为什么NSString会用copy修饰而不用Strong去修饰,首先先看我的另一片关于Strong的文章,里面介绍了strong修饰属性的问题。

    下面是copy需要注意的,首先先实现一个实例。

@interface CopyClass : NSObject

@property(copy,nonatomic)NSString *Name;

@property(copy,nonatomic)NSMutableString *MutableName;

@end

    将一个可变的NSMutableString test1赋值给Name、MutableName,再将test1的值改变,分别打印出来test1、Name、MutableName的内存地址。分析

NSMutableString *test1 = [[NSMutableString alloc]initWithString:@"test"];

CopyClass *testClass = [[CopyClass alloc]init];

testClass.Name = test1;

testClass.MutableName = test1;

[test1 appendString:@"add 哈哈"];

打印结果:

2018-02-01 01:11:40.060493+0800 PropertyTest[1699:292294] test1:0x10af30150--test哈哈哈

2018-02-01 01:11:40.060721+0800 PropertyTest[1699:292294] testClass.Name:0x10af30130--test

2018-02-01 01:11:40.060951+0800 PropertyTest[1699:292294] testClass.MutableName:0xa000000747365744--test

    只有可变的NSMutableString test1的值改变了其他的值都没有改变。说明了copy是深拷贝。

    将一个不可变的NSString test1赋值给Name、MutableName,再将test1的值改变,分别打印出来test1、Name、MutableName的内存地址。分析

NSString *test1 = @"test";

CopyClass *testClass = [[CopyClass alloc]init];

testClass.Name = test1;

testClass.MutableName = [[NSMutableString alloc]initWithString:test1];

test1 = @"test哈哈哈";

    打印结果:

2018-02-01 01:19:46.227394+0800 PropertyTest[1775:323385] test1:0x103701150--test哈哈哈

2018-02-01 01:19:46.227670+0800 PropertyTest[1775:323385] testClass.Name:0x103701130--test

2018-02-01 01:19:46.227900+0800 PropertyTest[1775:323385] testClass.MutableName:0xa000000747365744--test

     修改变量NSString Test1的值其他的值都没有改变。说明了copy是深拷贝。

需要注意的是:

    用copy修饰一个NSMutableString的属性时,将一个可变或不可变的变量的值赋值给它时,用这个属性修改值的内容会崩溃。实例分析

// NSString *test1 = @"test";

NSMutableString *test1 = [[NSMutableString alloc]initWithString:@"test"];

CopyClass *testClass = [[CopyClass alloc]init];

testClass.MutableName = [[NSMutableString alloc]initWithString:test1];

[testClass.MutableName appendString:@"ahhaha"];

NSLog(@"test1:%p--%@",test1,test1);

NSLog(@"testClass.MutableName:%p--%@",testClass.MutableName,testClass.MutableName);

崩溃日志:

2018-02-01 01:26:12.133086+0800 PropertyTest[1860:346864] -[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000747365744

2018-02-01 01:26:12.137246+0800 PropertyTest[1860:346864] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000747365744'

    分析:reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance   像NSTaggedPointerString这个实例发送appendString消息的时候没有找到这个方法,出现的崩溃。

    原因是copy修饰的属性最终的值都是不可变的类型,testClass.MutableName = [[NSMutableString alloc]initWithString:test1]; 这行代码最终生成的值是NSString类型。用NSString的实例去调用appendString出现的崩溃。  

    结论,用copy修饰的属性,都是不可变的变量。

自定义对象的copy

    实例:

CopyClass *testClass = [[CopyClass alloc]init];

testClass.Name = @"test";

testClass.MutableName = [[NSMutableString alloc]initWithString:@"test1"];

CopyClass *copytest = [testClass copy];

    直接崩溃:崩溃日志

2018-02-01 01:37:52.146383+0800 PropertyTest[1982:395784] -[CopyClass copyWithZone:]: unrecognized selector sent to instance 0x600000421c40

2018-02-01 01:37:52.152804+0800 PropertyTest[1982:395784] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CopyClass copyWithZone:]: unrecognized selector sent to instance 0x600000421c40'

    分析:实例当中没有找到 copyWithZone方法,在自定义的类里面实现这个方法

- (instancetype)copyWithZone:(NSZone *)zone {

CopyClass *copyModel = [[CopyClass allocWithZone:zone] init];

copyModel.Name = self.Name;

copyModel.MutableName = self.MutableName; return copyModel;

}

    再次运行完美。

总结

    属性copy关键字是深拷贝;

    注意:1.这个属性被赋值的时候,都会转换成不可变的类型。

                2.拷贝对象的时候需要重写copyWithZone:方法。


发散

    copyWithZone   是什么?

    挖坑 

你可能感兴趣的:(Property copy)