问题来源:

常见面试问题之:

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

最新文章

  1. redis持久化RDB和AOF
  2. python中threading的用法
  3. noi 1.5 45:金币
  4. UVA 572 (dfs)
  5. [ActionScript 3.0] AS3调用百度地图API
  6. vnext 技术两篇文章和评论
  7. ID生成器详解
  8. JavaScript的DOM操作(三)
  9. 模拟电路"虚短" & "虚断"
  10. 用Python对体积较大的CSV文件进行比较的经验
  11. nginx 502 Bad Gateway 错误问题收集
  12. android文件选择器、仿淘宝编辑页面、新手引导层等源码
  13. Linux虚拟机搭建本地yum源
  14. ACM-ICPC 2018 徐州赛区网络预赛 C Cacti Lottery(期望+暴力)
  15. Python3学习之路~8.5 SocketServer实现多并发
  16. C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)
  17. okhttp 内网可以有,但外网访问数据返不回来,代码一样
  18. win10ssh连接ubuntu服务器并本地绘图
  19. sharepoint 2010 记录管理 对象模型
  20. 学习笔记:Maven的ArcheType的学习笔记

热门文章

  1. JavaScript正则表达式-RegExp对象
  2. PAT Basic 1072
  3. joyoi1957 「Poetize5」Vani和Cl2捉迷藏
  4. HDU 3943 K-th Nya Number
  5. arc和mrc混用
  6. 2017NOIP初赛游记
  7. angular中关于ng-repeat的性能问题
  8. iOS7中Cell高度 Label高度自适应
  9. git多人协作--分支
  10. GitHub中watch、star、fork的作用