Cocoa root class
问题来源:
常见面试问题之:
NSObject和NSObject protocol有什么区别,为什么要有NSObject protocol, 有没有不继承自NSObject的类?
虽然在iOS开发过程中所用到的几乎所有的类都派生自NSObject, 但Cocoa并不是只有一个NSObject这一个root class. 另一个典型的root class就是NSProxy. 这也就是为什么同时需要定义NSObject类和NSObject协议. 那些不是从NSObject派生的类,也可以实现NSObject protocol的. 也就是说,NSObject class和NSObject protocol本身是相互独立的,按照正常的对类的理解和对协议的理解去理解这个事情就可以了。当然,NSObject protocol协议存在的必要性就是因为不是所有的类都派生自NSObject.
在NSProtocol的定义中有如下的方法:
- (BOOL)isProxy;
这个方法就可以用来判断这个类是否是从其他的根类派生.
文档描述如下:
- (BOOL)isProxy |
|
Description |
Returns a Boolean value that indicates whether the receiver does not descend from NSObject. (required) |
This method is necessary because sending isKindOfClass: or isMemberOfClass: to an NSProxy object will test the object the proxy stands in for, not the proxy itself. Use this method to test if the receiver is a proxy (or a member of some other root class). |
|
Returns |
NO if the receiver really descends from NSObject, otherwise YES. |
至于NSProxy本身,一般iOS开发中会比较少用。NSProxy可以用来实现代理模式和模拟多继承;具体的可以参见如下的博文:
使用NSProxy实现代理模式 : http://blog.csdn.net/onlyou930/article/details/7548476
使用NSProxy实现消息转发机制,模拟多重继承: http://blog.csdn.net/onlyou930/article/details/7548634
另有如下文章详细的描述NSObject class与NSObject protocol的,我比较懒,就直接贴链接了:
原文:NSObject: the Class and the Protocol https://mikeash.com/pyblog/friday-qa-2013-10-25-nsobject-the-class-and-the-protocol.html
译文:http://my.oschina.net/yongbin45/blog/202535
最新文章
- redis持久化RDB和AOF
- python中threading的用法
- noi 1.5 45:金币
- UVA 572 (dfs)
- [ActionScript 3.0] AS3调用百度地图API
- vnext 技术两篇文章和评论
- ID生成器详解
- JavaScript的DOM操作(三)
- 模拟电路";虚短"; &; ";虚断";
- 用Python对体积较大的CSV文件进行比较的经验
- nginx 502 Bad Gateway 错误问题收集
- android文件选择器、仿淘宝编辑页面、新手引导层等源码
- Linux虚拟机搭建本地yum源
- ACM-ICPC 2018 徐州赛区网络预赛 C Cacti Lottery(期望+暴力)
- Python3学习之路~8.5 SocketServer实现多并发
- C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)
- okhttp 内网可以有,但外网访问数据返不回来,代码一样
- win10ssh连接ubuntu服务器并本地绘图
- sharepoint 2010 记录管理 对象模型
- 学习笔记:Maven的ArcheType的学习笔记