NSString为什么要用copy关键字,如果用strong会有什么问题 OC中的深拷贝与浅拷贝

首先说一下深拷贝和浅拷贝,深拷贝是内存拷贝,浅拷贝是指针拷贝

写代码的时候有两个copy方法

- (id)copy;

- (id)mutableCopy;

copy出的对象为不可变类型        mutableCopy出的对象为可变类型

NSString

    NSString *sourceString = [NSString stringWithFormat:@"youyouyou"];

    //不产生新的内存空间

    NSString *copyStr = [sourceString copy];

    //产生新的内存空间

    NSMutableString *mutableStr = [sourceString mutableCopy];

    NSLog(@"sourceString : %@    %p",sourceString,sourceString);

    NSLog(@"copyStr : %@    %p",copyStr,copyStr);

    NSLog(@"mutableStr : %@    %p",mutableStr,mutableStr);

运行结果

2017-06-30 09:59:42.695 ttt[780:22226] sourceString : youyouyou    0xa250d09434250d09

2017-06-30 09:59:42.696 ttt[780:22226] copyStr : youyouyou    0xa250d09434250d09

2017-06-30 09:59:42.696 ttt[780:22226] mutableStr : youyouyou    0x608000261f00

可以看出 对于NSString对象来说copy不会产生新的内存地址    mutableCopy产生新的内存地址        产生新内存地址的是深拷贝  否则是浅拷贝

NSMutableString

    NSMutableString *sourceString = [NSMutableString stringWithFormat:@"youyouyou"];

    //产生新的内存空间

    NSString *copyStr = [sourceString copy];

    //产生新的内存空间

    NSMutableString *mutableStr = [sourceString mutableCopy];

    NSLog(@"sourceString : %@    %p",sourceString,sourceString);

    NSLog(@"copyStr : %@    %p",copyStr,copyStr);

    NSLog(@"mutableStr : %@    %p",mutableStr,mutableStr);

运行结果

2017-06-30 10:04:48.601 ttt[799:24932] sourceString : youyouyou    0x60000007e280

2017-06-30 10:04:48.601 ttt[799:24932] copyStr : youyouyou    0xa250d09434250d09

2017-06-30 10:04:48.601 ttt[799:24932] mutableStr : youyouyou    0x60000007e2c0

对于NSMuatbleString copy 和mutableCopy 都产生新的内存   都是深拷贝


然后来说作为属性时,用copy还是strong

建一个Person类测试

#import

@interface Person : NSObject

@property (nonatomic, copy) NSString *copyString;

@property (nonatomic, strong) NSString *strongString;

对NSString测试代码

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

    NSString *sourceString = [NSString stringWithFormat:@"youyouyou"];

    person.copyString = sourceString;

    person.strongString = sourceString;

    sourceString = @"aaa";

    NSLog(@"sourceString : %@    %p",sourceString,sourceString);

    NSLog(@"person.copyString : %@    %p",person.copyString,person.copyString);

    NSLog(@"person.strongString : %@    %p",person.strongString,person.strongString);

运行结果

2017-06-30 10:29:50.926 ttt[920:36840] sourceString : aaa    0x10f53f0f8

2017-06-30 10:29:50.926 ttt[920:36840] person.copyString : youyouyou    0xa250d09434250d09

2017-06-30 10:29:50.927 ttt[920:36840] person.strongString : youyouyou    0xa250d09434250d09

这样一看strong和copy的效果一样   改变源字符串后都不会影响,这是因为源字符串是不可变的(指向的内存地址不可变,NSString表示的是意思是内存地址不可变)  把源字符串赋值给person.copyString 和person.strongString后他们三个一起指向@“youyouyou”的内存地址(可以理解为内存地址的赋值)      后来源字符串变为@"aaa" (这个变量的指针指向了另外一块内存地址,之前指向的那块内存地址是不可以变化的,但是这个变量可以指向其他的内存地址)  源字符串就指向了@“aaa”的内存地址  而person.copyString 和person.strongString仍然指向@“youyouyou”的内存地址 所以不会改变

对NSMutableString测试代码

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

    NSMutableString *sourceString = [NSMutableString stringWithFormat:@"youyouyou"];

    person.copyString = sourceString;

    person.strongString = sourceString;

    [sourceString appendString:@"aaa"];

    NSLog(@"sourceString : %@    %p",sourceString,sourceString);

    NSLog(@"person.copyString : %@    %p",person.copyString,person.copyString);

    NSLog(@"person.strongString : %@    %p",person.strongString,person.strongString);

运行结果

2017-06-30 10:44:16.771 ttt[946:42747] sourceString : youyouyouaaa    0x6080002738c0

2017-06-30 10:44:16.772 ttt[946:42747] person.copyString : youyouyou    0xa250d09434250d09

2017-06-30 10:44:16.772 ttt[946:42747] person.strongString : youyouyouaaa    0x6080002738c0

这次可以看到赋值完后,改变源字符串,copy修饰的不变,而strong修饰的变了,这是因为对NSMutableString执行copy方法是深拷贝,开辟了新的内存,copy修饰的属性内存地址变了     ,而strong只是引用计数+1内存地址还是源字符串的,所以改变影响不到copy修饰的属性 


copy  和 strong 修饰的set方法如下

//copy修饰的

//当外界给属性赋值时,会调用该属性的set方法来赋值,然后在该set方法里面,外界传进来的字符串会调用copy方法

- (void)setScopyString:(NSString *)scopyString {

    _scopyString = [scopyString copy];

}

//strong修饰的

- (void)setStrongString:(NSString *)strongString {

    _strongString = strongString;

}

这样就可以看出来当源字符串是 可变类型的时候  对其执行copy方法是深拷贝

所以总结一下

当源字符串是不可变类型时      copy  strong   改变源字符串 效果一样

当源字符串是可变类型时      copy  修饰的是深拷贝   改变源字符串  不影响copy的属性内容      strong修饰的属性 就随着源字符串的改变而变了

https://blog.csdn.net/qq_34900204/article/details/73932866

你可能感兴趣的:(NSString为什么要用copy关键字,如果用strong会有什么问题 OC中的深拷贝与浅拷贝)