博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Object copying
阅读量:7081 次
发布时间:2019-06-28

本文共 1249 字,大约阅读时间需要 4 分钟。

NSObject类定义了copy方法,但这只是一个convinence method for classes who adopt NSCopying协议。如果某个类没有实现copyWithZone:,那调用copy方法会抛出异常。NSObject类的方法mutableCopy对应于NSMutableCopying protocol的方法mutableCopyWithZone:

NSObject does not itself support the NSCopying protocol. Subclasses must support the protocol and implement the copyWithZone: method. A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject.

如果一个类考虑adopt NSCopying协议,那还要考虑是否应该选择NSMutableCopying?如何选择呢?先看官方文档。

The returned object is implicitly retained by the sender, who is responsible for releasing it. The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

也就是说,对于一个类/对象来说,在考虑adopt NSCopying或者NSMutableCopying协议时,需要考虑receiving object是由有“immutable vs. mutable”之分,如果有此区分,那copyWithZone:返回的是immutable的copy,mutable的copy由mutableCopyWithZone:实现。如果没有“immutable vs. mutable”之分,那就不要实现mutableCopyWithZone:, 并且从通常意义上来考虑copy行为是什么样的,毕竟每个类的Copy行为是不一样的,比如有的是做shallow copy,有的是做deep copy,或者部分shallo,部分deep,完全依需求而定。

 

转载于:https://www.cnblogs.com/whyandinside/archive/2013/03/13/2958152.html

你可能感兴趣的文章
kafka 主要内容介绍
查看>>
Linux获取网页源码的几种方法
查看>>
write a python http server & client
查看>>
并非全部的程序猿都适合做技术管理
查看>>
jQuery 效果 - 淡入淡出
查看>>
SSDB图形界面管理工具:phpssdbadmin安装部署
查看>>
how to backup and restore database of SQL Server
查看>>
Hibernate- QBC查询方式
查看>>
【Linux】linux查看日志文件内容命令tail、cat、tac、head、echo
查看>>
php中的或运算
查看>>
位图(BitMap)索引
查看>>
CSS3伪类和伪元素的特性和区别
查看>>
vue实现文章内容过长点击阅读全文功能
查看>>
记一次elementUI Icon 加载无效的问题。并且提示错误 Failed to decode downloaded font:
查看>>
OpenGL之位图的绘制和gluOrtho2D等函数详解
查看>>
Linux磁盘概念及其管理工具fdisk
查看>>
Linux epoll版定时器
查看>>
objective C中数据持久化方式1--对象归档
查看>>
Python面向对象编程 - 一个记事本程序范例(一)
查看>>
马桶餐厅
查看>>