NSString *str1=@"hello";
NSString *str2=[str1 copy];//指针copy
NSString *str3=[str1 mutableCopy];//深copy
NSMutableString *mutableStr2=[str1
copy];//指针copy
NSMutableString *mutableStr3=[str1
mutableCopy];//深copy
[mutableStr3 appendString:@"-oc"];
NSLog(@"str1=%@\nstr2=%@\nstr3=%@\nmutablStr2=%@\nmutableStr3=%@\n\n\n\n\n",str1,str2,str3,mutableStr2,mutableStr3);
NSLog(@"str1=%p\nstr2=%p\nstr3=%p\nmutablStr2=%p\nmutableStr3=%p",str1,str2,str3,mutableStr2,mutableStr3);
2.对可变的字符串进行拷贝操作:
NSMutableString *m1 = [NSMutableString
stringWithFormat:@"%@",@"hello"];
NSMutableString *m2 = [m1
copy];//深copy
NSMutableString *m3 = [m1
mutableCopy];//深copy
NSString *strm2 = [m1
copy];//深copy
NSString *strm3 = [m1
mutableCopy];//深copy
NSLog(@"m1=%@\nm2=%@\nm3=%@\nstrm2=%@\nstrm3=%@\n\n",m1,m2,m3,strm2,strm3);
NSLog(@"m1=%p\nm2=%p\nm3=%p\nstrm2=%p\nstrm3=%p\n\n\n\n\n",m1,m2,m3,strm2,strm3);
//对于源数据为可变的字符串我执行copy
或者mutablecopy他们的地址都是新开辟的
[m1 setString:@"word world"];
NSLog(@"m1=%@\nm2=%@\nm3=%@\nstrm2=%@\nstrm3=%@\n\n",m1,m2,m3,strm2,strm3);
NSLog(@"m1=%p\nm2=%p\nm3=%p\nstrm2=%p\nstrm3=%p",m1,m2,m3,strm2,strm3);
结语:对于系统的非容器类对象,我们可以认为,如果对一不可变对象复制,copy是指针复制(浅拷贝)和mutableCopy就是对象复制(深拷贝)。如果是对可变对象复制,都是深拷贝,但是copy返回的对象是不可变的。