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 <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *copyStr;
@property (nonatomic, strong) NSString *strongStr;

对NSString测试代码

    Person *person = [[Person alloc]init];
    
    NSString *sourceString = [NSString stringWithFormat:@"youyouyou"];
    
    person.scopyString = sourceString;
    person.strongString = sourceString;
    
    sourceString = @"aaa";

    NSLog(@"sourceString : %@     %p",sourceString,sourceString);
    NSLog(@"person.scopyString : %@     %p",person.scopyString,person.scopyString);
    NSLog(@"person.strongStr : %@     %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.scopyString : youyouyou     0xa250d09434250d09
2017-06-30 10:29:50.927 ttt[920:36840] person.strongStr : youyouyou     0xa250d09434250d09

这样一看strong和copy的效果一样   改变源字符串后都不会影响,这是因为源字符串是不可变的  把源字符串赋值给person.scopyString 和person.strongString后他们三个一起指向@“youyouyou”的内存地址       后来源字符串变为@"aaa"   源字符串就指向了@“aaa”的内存地址  而person.scopyString 和person.strongString仍然指向@“youyouyou”的内存地址 所以不会改变


对NSMutableString测试代码

    Person *person = [[Person alloc]init];
    
    NSMutableString *sourceString = [NSMutableString stringWithFormat:@"youyouyou"];
    
    person.scopyString = sourceString;
    person.strongString = sourceString;
    
    [sourceString appendString:@"aaa"];

    NSLog(@"sourceString : %@     %p",sourceString,sourceString);
    NSLog(@"person.scopyString : %@     %p",person.scopyString,person.scopyString);
    NSLog(@"person.strongStr : %@     %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.scopyString : youyouyou     0xa250d09434250d09
2017-06-30 10:44:16.772 ttt[946:42747] person.strongStr : youyouyouaaa     0x6080002738c0

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

copy  和 strong 修饰的set方法如下

//copy修饰的
- (void)setScopyString:(NSString *)scopyString {
    _scopyString = [scopyString copy];
}

//strong修饰的
- (void)setStrongString:(NSString *)strongString {
    _strongString = strongString;
}

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


所以总结一下

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

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





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值