所谓copy
来源:互联网 发布:淘宝买东西返钱的软件 编辑:程序博客网 时间:2024/06/07 18:41
浅层复制:只复制指向对象的指针,而不复制引用对象本身。
深层复制:复制引用对象本身
copy和mutableCopy的区别在于它们的返回值是其中前者是不可变的类,后者是可变的类.
不管被复制的对象是不可变的还是可变的,copy的返回值类型始终是不可变类型。
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) { NSString *str1 = @"hello"; NSString *str2 = [str1 copy];//指针copy NSString *str3 = [str1 mutableCopy];//深copy NSLog(@"str1 = %@ str1 = %p",str1,str1); NSLog(@"str2 = %@ str2 = %p",str2,str2); NSLog(@"str3 = %@ str3 = %p",str3,str3); NSMutableString *mutableStr2 = [str1 copy];//指针copy NSMutableString *mutableStr3 = [str1 mutableCopy];//深copy NSMutableString *mutableStr4 = [str1 mutableCopy]; [mutableStr4 appendString:@"-OC"]; NSLog(@"mutableStr2 = %@ mutableStr2 = %p",mutableStr2,mutableStr2); NSLog(@"mutableStr3 = %@ mutableStr3 = %p",mutableStr3,mutableStr3); NSLog(@"mutableStr4 = %@ mutableStr4 = %p",mutableStr4,mutableStr4); return 0;}
</pre><pre name="code" class="objc"><pre name="code" class="html">2015-11-06 11:09:03.589 msDemo[1301:43528] str1 = hello str1 = 0x1000010302015-11-06 11:09:03.590 msDemo[1301:43528] str2 = hello str2 = 0x1000010302015-11-06 11:09:03.590 msDemo[1301:43528] str3 = hello str3 = 0x1001145c02015-11-06 11:09:03.590 msDemo[1301:43528] mutableStr2 = hello mutableStr2 = 0x1000010302015-11-06 11:09:03.590 msDemo[1301:43528] mutableStr3 = hello mutableStr3 = 0x1001149d02015-11-06 11:09:03.590 msDemo[1301:43528] mutableStr4 = hello-OC mutableStr4 = 0x1001148d0
</pre><pre name="code" class="objc"><pre name="code" class="objc">2015-11-06 11:14:15.262 msDemo[1309:45570] str1 = hello str1 = 0x1000010302015-11-06 11:14:15.330 msDemo[1309:45570] str2 = hello str2 = 0x1000010302015-11-06 11:14:15.331 msDemo[1309:45570] str3 = hello str3 = 0x100206ba02015-11-06 11:14:15.331 msDemo[1309:45570] mutableStr2 = hello mutableStr2 = 0x1000010302015-11-06 11:14:15.331 msDemo[1309:45570] mutableStr3 = hello mutableStr3 = 0x100206eb02015-11-06 11:14:15.331 msDemo[1309:45570] mutableStr4 = hello-OC mutableStr4 = 0x100207030
</pre><pre name="code" class="objc"><pre name="code" class="objc">2015-11-06 11:14:41.343 msDemo[1313:45789] str1 = hello str1 = 0x1000010302015-11-06 11:14:41.344 msDemo[1313:45789] str2 = hello str2 = 0x1000010302015-11-06 11:14:41.344 msDemo[1313:45789] str3 = hello str3 = 0x100206ba02015-11-06 11:14:41.344 msDemo[1313:45789] mutableStr2 = hello mutableStr2 = 0x1000010302015-11-06 11:14:41.344 msDemo[1313:45789] mutableStr3 = hello mutableStr3 = 0x100206cc02015-11-06 11:14:41.344 msDemo[1313:45789] mutableStr4 = hello-OC mutableStr4 = 0x100206c00
此处特别给出了三次运行结果,可以看出mutableCopy每次复制的地址都不一样,他复制的地址是每次随机复制内存的地址,是深copy,Copy每次运行的结果都相同,都是0x100001030,他只是指针的Copy,不管是NSString还是NSMutableString类型
0 0
- 所谓copy
- 所谓的写时复制-Dll的copy-on-write技术
- 所谓老公 所谓老婆
- 所谓科学,所谓假设
- 所谓时光,所谓爱
- 所谓时光,所谓爱
- copy
- copy
- copy
- copy.
- copy
- copy
- copy
- copy
- copy
- copy
- Copy
- copy
- Linux常用的网络命令
- 网络基础GET的部分代码
- 排列与组合算法
- Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法
- Malformed input or input contains unmappable chacraters导致编译失败问题解决
- 所谓copy
- kmalloc kfree学习笔记
- Ubuntu与win7系统通过Samba共享目录
- 取得radio或者checked值的方法
- Android 5.0/6.0进程自动重启调研(5.0+)
- git初始化和建立项目
- MySQL存储过程传参之in,out,inout参数用法
- 图片选取器的使用
- 推荐IT人可以使用的网站