IOS学习笔记十八(copy、mutableCopy、NSCopying、NSMutableCopy、深复制、浅复制)
1、 copy、mutableCopy方法
copy方法返回对象的不可修改的副本
mutableCopy方法返回的对象可修改的副本
1)、测试demo
int main(int argc, char * argv[]) {@autoreleasepool {NSMutableString *book = [NSMutableString stringWithString:@"chenyu"];NSMutableString *bookCopy = [book mutableCopy];[bookCopy replaceCharactersInRange:NSMakeRange(1, 2) withString:@"gong"];NSLog(@"book is %@", book);NSLog(@"bookCopy is %@", bookCopy);NSString *str = @"chenyu";NSMutableString *strCopy = [str mutableCopy];[strCopy appendString:@"chenyu"];NSLog(@"strCopy is:%@", strCopy);//由于str2是不可变的,所以运行下面会报错NSMutableString *str2 = [str copy];NSLog(@"str copy chen is:%@", str2);
// [str2 appendString:@"chenyu"];}
}
2)、运行结果
2018-07-15 19:03:35.564049+0800 cyTest[27254:9418105] book is chenyu
2018-07-15 19:03:35.565157+0800 cyTest[27254:9418105] bookCopy is cgongnyu
2018-07-15 19:03:35.566056+0800 cyTest[27254:9418105] strCopy is:chenyuchenyu
2018-07-15 19:03:35.566857+0800 cyTest[27254:9418105] str copy chen is:chenyu
2、NSCopying、NSMutableCopy协议
对象调用mutableCopy方法来复制自身的可变副本,需要类实现NSMutbaleCopying协议,让类实现mutableCopyWithZone方法demo可以看下下面写的浅复制
3、深复制和浅复制
我个人理解感觉浅复制是复制后的属性(指针变量)指向的地址都是同一块地址,当复制后的对象属性值发生变化的时候,原始值也发生了变化。
深复制就是复制后的属性(指针变量)指向的地址不是同一块地址,当复制后的对象属性值发生变化的时候,原始值不会发生变化。
1)、浅复制测试Demo
Dog.h
#import <Foundation/Foundation.h>
#ifndef Dog_h
#define Dog_h@interface Dog : NSObject<NSCopying>
@property (nonatomic, strong) NSMutableString *name;
@property (nonatomic, assign) int age;
@end
#endif /* Dog_h */
Dog.m
#import <Foundation/Foundation.h>
#import "Dog.h"@implementation Dog
@synthesize name;
@synthesize age;
-(id)copyWithZone:(NSZone *)zone
{Dog *dog = [[[self class] allocWithZone:zone] init];dog.name = self.name;dog.age = self.age;return dog;
}
@end
main.m
Dog *dog1 = [Dog new];dog1.name = [NSMutableString stringWithString:@"chen"];dog1.age = 1;NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);//浅复制Dog *dog2 = [dog1 copy];
// dog2.name = [NSMutableString stringWithString:@"li"];[dog2.name replaceCharactersInRange:NSMakeRange(1, 2) withString:@"hel"];dog2.age = 20;NSLog(@"dog2.name is %@ and age is %d", dog2.name, dog2.age);NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);
2)、运行结果
2018-07-15 19:03:35.567556+0800 cyTest[27254:9418105] dog1.name is chen and age is 1
2018-07-15 19:03:35.567690+0800 cyTest[27254:9418105] dog2.name is cheln and age is 20
2018-07-15 19:03:35.567768+0800 cyTest[27254:9418105] dog1.name is cheln and age is 1
Dog.h文件一样
Dog.m文件有点变化
#import <Foundation/Foundation.h>
#import "Dog.h"@implementation Dog
@synthesize name;
@synthesize age;
-(id)copyWithZone:(NSZone *)zone
{Dog *dog = [[[self class] allocWithZone:zone] init];
// dog.name = self.name;dog.name = [self.name mutableCopy];dog.age = self.age;return dog;
}
@end
main.m文件一样
Dog *dog1 = [Dog new];dog1.name = [NSMutableString stringWithString:@"chen"];dog1.age = 1;NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);//深复制Dog *dog2 = [dog1 copy];
// dog2.name = [NSMutableString stringWithString:@"li"];[dog2.name replaceCharactersInRange:NSMakeRange(1, 2) withString:@"hel"];dog2.age = 20;NSLog(@"dog2.name is %@ and age is %d", dog2.name, dog2.age);NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);
4)、运行结果
2018-07-15 19:13:08.751335+0800 cyTest[27529:9427019] dog1.name is chen and age is 1
2018-07-15 19:13:08.751686+0800 cyTest[27529:9427019] dog2.name is cheln and age is 20
2018-07-15 19:13:08.751885+0800 cyTest[27529:9427019] dog1.name is chen and age is 1
4、setter方法的复制选项
我们setter方法复制选项的时候,如果加了copy属性,比如setName方法
-(void)setName : (NSMutableStrinfg *)name
{self.name = [name copy];}
#import <Foundation/Foundation.h>
#import "Dog.h"@implementation Dog
@synthesize name;
@synthesize age;
@end
所以当对象设置了name属性的话,就不能再改变了,如下demo
Dog.h
#import <Foundation/Foundation.h>
#ifndef Dog_h
#define Dog_h@interface Dog : NSObject
@property (nonatomic, copy) NSMutableString *name;
@property (nonatomic, assign) int age;
@end
#endif /* Dog_h */
Dog.m
#import <Foundation/Foundation.h>
#import "Dog.h"@implementation Dog
@synthesize name;
@synthesize age;
@end
main.m
Dog *dog1 = [Dog new];dog1.name = [NSMutableString stringWithString:@"chen"];dog1.age = 1;[dog1.name appendString:@"hello"];
运行结果
2018-07-15 19:22:17.736557+0800 cyTest[27853:9436238] -[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa0000006e6568634
2018-07-15 19:22:17.739655+0800 cyTest[27853:9436238] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa0000006e6568634'
*** First throw call stack:
(0 CoreFoundation 0x0000000104fd61e6 __exceptionPreprocess + 2941 libobjc.A.dylib 0x000000010466b031 objc_exception_throw + 482 CoreFoundation 0x0000000105057784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 1323 CoreFoundation 0x0000000104f58898 ___forwarding___ + 14324 CoreFoundation 0x0000000104f58278 _CF_forwarding_prep_0 + 1205 cyTest 0x0000000103d60438 main + 6006 libdyld.dylib 0x0000000108a2f955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
很明显,报错了。
IOS学习笔记十八(copy、mutableCopy、NSCopying、NSMutableCopy、深复制、浅复制)相关推荐
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置...
python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置 Download JetBrains Python IDE :: PyCharm http://ww ...
- windows内核开发学习笔记十八:IRP 处理的标准模式
windows内核开发学习笔记十八:IRP 处理的标准模式 在 Windows 内核中的请求基本上是通过 I/O Request Packet 完成的. I/O manager ---> Dis ...
- Polyworks脚本开发学习笔记(十八)-用SDK开发Polyworks插件
Polyworks脚本开发学习笔记(十八)-用SDK开发Polyworks插件 插件是由PolyWorks加载的动态链接库(DLL文件),然后查询Polyworks模块,以确定它们具有哪些功能,提供给 ...
- 学习笔记(十八):MoRe-Fi用深度学习网络从非线性信号中恢复呼吸波形
<MoRe-Fi: Motion-robust and Fine-grained Respiration Monitoring via Deep-Learning UWB Radar>学习 ...
- 【D3D11游戏编程】学习笔记十八:模板缓冲区的使用、镜子的实现
(注:[D3D11游戏编程]学习笔记系列由CSDN作者BonChoix所写,转载请注明出处:http://blog.csdn.net/BonChoix,谢谢~) 模板缓冲区(Stencil Buffe ...
- three.js学习笔记(十八)——调整材质
介绍 到现在为止,我们都在创建新的着色器材质,但是如果我们想要修改一个Three.js内置的材质呢?或许我们对MeshStandardMaterial的处理结果感到满意,但是希望往里边添加顶点动画. ...
- 【theano-windows】学习笔记十八——混合蒙特卡洛采样
#前言 继续之前的Theano学习,本次主要学习混合蒙特卡洛(Hybrid Monte-Carlo Sampling)采样算法. 国际惯例,参考网址 Hybrid Monte-Carlo Sampli ...
- opencv学习笔记十八:模板匹配(cv2.matchTemplate、cv2.minMaxLoc)
1.目标匹配函数:cv2.matchTemplate() res=cv2.matchTemplate(image, templ, method, result=None, mask=None) ima ...
- JavaScript权威设计--事件冒泡,捕获,事件句柄,事件源,事件对象(简要学习笔记十八)...
1.事件冒泡与事件捕获 2.事件与事件句柄 3.事件委托:利用事件的冒泡技术.子元素的事件最终会冒泡到父元素直到跟节点.事件监听会分析从子元素冒泡上来的事件. 事件委托的好处: 1.每个函 ...
最新文章
- 相机标定:(1)相机模型
- 神经网络之dropout层
- SAP CRM WebClient UI Context node expose条件
- 两年15.5k star只是新起点,涛思数据正式宣布TDengine灯塔计划
- 【Oracle】11g外部表指定oracle_datapump引擎,不能使用preprocessor预处理子句。
- .net体系结构——C#高级编程第一章
- 【数论】蓝桥2018:第几个幸运数
- chrome谷歌浏览器 离线安装包下载64位
- 简单的朴素贝叶斯算法实现英文文本分类(Python实现)
- word---尾注法插入参考文献
- Mac邮件客户端(Edison Mail)的功能特点
- 阿里云香港服务器带宽太贵怎么办?香港节点全球CDN加速你值得拥有
- Fragment already added解决
- 用fiddler+chrome搞定在线学习网站
- 部署本地thinkphp6(iis+php7)
- js 网页做一个简单的计算器
- C++删除文件末尾的空行
- 医学影像配准 NCC Loss
- 开源工作流BPM软件JFlow安装配置视频教程
- 液晶面板里面有些什么配件_液晶电视核心部件各类液晶面板介绍是什么?请生意经的朋友帮忙解答...
热门文章
- c#爬虫-解决ChromeDriver 版本问题
- 使用 Redis Stream 实现消息队列
- 动手造轮子:实现一个简单的基于 Console 的日志输出
- Amazing 2020
- 如何使用 C# 中的 Tuple
- ASP.NET Core Cookie SameSite
- 基于.NetCore3.1搭建项目系列 —— 使用Swagger做Api文档 (下篇)
- 如何运用领域驱动设计 - 领域服务
- 【学习笔记】Docker - 02. 在容器中运行软件(上)
- .NET World——gPRC概览