首先说一下深拷贝和浅拷贝,深拷贝是内存拷贝,浅拷贝是指针拷贝
写代码的时候有两个copy方法
- (id)copy;
- (id)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
然后来说作为属性时,用copy还是strong
建一个Person类测试
#import
@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
对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 修饰的set方法如下
//copy修饰的
- (void)setScopyString:(NSString *)scopyString {
_scopyString = [scopyString copy];
}
//strong修饰的
- (void)setStrongString:(NSString *)strongString {
_strongString = strongString;
}
当源字符串是不可变类型时 copy strong 改变源字符串 效果一样
当源字符串是可变类型时 copy 修饰的是深拷贝 改变源字符串 不影响copy的属性内容 strong修饰的属性 就随着源字符串的改变而变了