关于copy
2015-07-02 00:00
316 查看
摘要: 深拷贝,浅拷贝 copy
NSMutableArray* a = [NSMutableArray arrayWithObjects:@"123",@"345", nil];
NSMutableArray* b = [NSMutableArray arrayWithObjects:@"xyz",@"abc", nil];
NSArray *c = [NSArray arrayWithObjects:a, b, nil];
NSArray *d = [c copy];
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
a[0] = @"haha";
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
结果:
2015-07-02 13:53:00.112 Test[15564:327797] element: 123
2015-07-02 13:53:00.112 Test[15564:327797] element: 345
2015-07-02 13:53:00.112 Test[15564:327797] element: xyz
2015-07-02 13:53:00.112 Test[15564:327797] element: abc
2015-07-02 13:53:00.112 Test[15564:327797] element: haha
2015-07-02 13:53:00.112 Test[15564:327797] element: 345
2015-07-02 13:53:00.112 Test[15564:327797] element: xyz
2015-07-02 13:53:00.113 Test[15564:327797] element: abc
******************************************
NSMutableArray* a = [NSMutableArray arrayWithObjects:@"123",@"345", nil];
NSMutableArray* b = [NSMutableArray arrayWithObjects:@"xyz",@"abc", nil];
NSArray *c = [NSArray arrayWithObjects:a, b, nil];
// SEE the difference.
NSArray *d = [c copyDeeply];
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
a[0] = @"haha";
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
结果:
2015-07-02 13:53:47.111 Test[15596:328543] element: 123
2015-07-02 13:53:47.112 Test[15596:328543] element: 345
2015-07-02 13:53:47.112 Test[15596:328543] element: xyz
2015-07-02 13:53:47.112 Test[15596:328543] element: abc
2015-07-02 13:53:47.112 Test[15596:328543] element: 123
2015-07-02 13:53:47.112 Test[15596:328543] element: 345
2015-07-02 13:53:47.112 Test[15596:328543] element: xyz
2015-07-02 13:53:47.112 Test[15596:328543] element: abc
copy只做第一级的深拷贝。 如果array里面存的是指针,它就会把指针值做深拷贝,等于是后面的数组跟前面的数组,存的指针值是一样的,但是指针指向的内容不做深拷贝,所以改了指针指向的内容,会同时影响两个数组。
NSMutableArray* a = [NSMutableArray arrayWithObjects:@"123",@"345", nil];
NSMutableArray* b = [NSMutableArray arrayWithObjects:@"xyz",@"abc", nil];
NSArray *c = [NSArray arrayWithObjects:a, b, nil];
NSArray *d = [c copy];
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
a[0] = @"haha";
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
结果:
2015-07-02 13:53:00.112 Test[15564:327797] element: 123
2015-07-02 13:53:00.112 Test[15564:327797] element: 345
2015-07-02 13:53:00.112 Test[15564:327797] element: xyz
2015-07-02 13:53:00.112 Test[15564:327797] element: abc
2015-07-02 13:53:00.112 Test[15564:327797] element: haha
2015-07-02 13:53:00.112 Test[15564:327797] element: 345
2015-07-02 13:53:00.112 Test[15564:327797] element: xyz
2015-07-02 13:53:00.113 Test[15564:327797] element: abc
******************************************
NSMutableArray* a = [NSMutableArray arrayWithObjects:@"123",@"345", nil];
NSMutableArray* b = [NSMutableArray arrayWithObjects:@"xyz",@"abc", nil];
NSArray *c = [NSArray arrayWithObjects:a, b, nil];
// SEE the difference.
NSArray *d = [c copyDeeply];
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
a[0] = @"haha";
for (NSArray* arr in d) {
for (NSString* s in arr) {
NSLog(@"element: %@", s);
}
}
结果:
2015-07-02 13:53:47.111 Test[15596:328543] element: 123
2015-07-02 13:53:47.112 Test[15596:328543] element: 345
2015-07-02 13:53:47.112 Test[15596:328543] element: xyz
2015-07-02 13:53:47.112 Test[15596:328543] element: abc
2015-07-02 13:53:47.112 Test[15596:328543] element: 123
2015-07-02 13:53:47.112 Test[15596:328543] element: 345
2015-07-02 13:53:47.112 Test[15596:328543] element: xyz
2015-07-02 13:53:47.112 Test[15596:328543] element: abc
copy只做第一级的深拷贝。 如果array里面存的是指针,它就会把指针值做深拷贝,等于是后面的数组跟前面的数组,存的指针值是一样的,但是指针指向的内容不做深拷贝,所以改了指针指向的内容,会同时影响两个数组。
相关文章推荐
- SHINE OPENCART 自适应 多用途主题模板 ABC-0021
- ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge
- Nginx反向代理和负载均衡部署指南
- 10个工具让你的 shell 脚本更强大
- 10个工具让你的 shell 脚本更强大
- CentOS6.6 安装与配置 MySQL Cluster 7.4.4
- 一步一步跟着官方文档安装部署Openstack(icehouse)二
- CentOS 6.5 SSH命令无法执行的问题的解决办法
- RedHat系统配置笔记
- linux内存源码分析 - 伙伴系统(初始化和申请页框)
- rop 文件上传
- Linux平台的4个最佳开源代码编辑器
- Linux fork 后 wait 获取子进程结束的状态示例
- RHEL6.5/Centos6.5 搭建bugzilla
- 用虚拟机安装、启动、停用hadoop2.6.0(实现hdfsHA和ResourceManagerHA)步骤小记
- Xshell添加ssh隧道SOCKS代理
- Hadoop自定义计数器的使用
- How to Install Kali Linux on Android - Tutorial With Screenshot
- linux生成奔跑着的火车有趣动画步骤
- linux中级教程-中级shell