看见一到面试题讲述一下Objective-C中的isa?完全没听说过,打算小研究一下。

  参考:http://blog.sina.com.cn/s/blog_7a2ffd5c01010nme.html

  原来isa类似类似于java中的class,用于动态的获取一个类动态运行时的信息。不过OC比java牛B的是,OC可以在运行时为类添加方法,所以isa比class稍微复杂一点。

  看一下NSObject类的.h  

 @interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}

  NSObject类作为所有OC对象的父类只有这一个属性isa,我们有必要研究一下,它究竟是什么。

  官方介绍是这样的:

  Every object is connected to the run-time system through its isa instance variable, inherited from the NSObject class. isa identifies the object's class; it points to a structure that's compiled from the class definition. Through isa, an object can find whatever information it needs at runtime such as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages.

  可见,通过一个实例对象(Object)的isa,我们可以找到一个对象的所以信息,如类属性的结构,类方法(消息)的入口地址等。

  我们再来看一下Class类

struct objc_class {
Class isa OBJC_ISA_AVAILABILITY; #if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif } OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */

  我们发现,objc_class结构体中,还有一个Class属性,它是什么作用呢?对象的类(Class)的isa指向了metaclass。通过它我们可以找到静态变量和方法,还有在这个类里动态添加的类别方法。

  Object-C对类对象与实例对象中的 isa 所指向的类结构作了不同的命名:类对象中的 isa 指向类结构被称作 metaclass,metaclass 存储类的static类成员变量与static类成员方法(+开头的方法);实例对象中的 isa 指向类结构称作 class(普通的),class 结构存储类的普通成员变量与普通成员方法(-开头的方法)。

最新文章

  1. big-endian和little-endian
  2. 钉钉服务器端SDK PHP版
  3. JQuery面试题答案
  4. jQuery从主页面存取控制 iframe 中的元素,参数及方法
  5. oracle database resident connection pooling(驻留连接池)
  6. libc.so.6 误删后修复
  7. 九个Console命令,让 JS 调试更简单
  8. IOS10.3上传照片只能拍照不能选择解决办法
  9. codeforces 13 b
  10. Kafka笔记8(管理Kafka)
  11. gcc:call to &#39;__open_missing_mode&#39; declared with attribute error
  12. python的变量与注释
  13. 转:The Difference Between a LayoutTransform and a RenderTransform
  14. CSS之img标签
  15. c++11 线程池学习笔记 (二) 线程池
  16. 联通GWH-01路由猫超级用户登录方法
  17. 项目适配iOS9遇到的一些问题及解决办法(更新两个小问题)
  18. 国内云计算的缺失环节: GPU并行计算(转)
  19. September 09th 2017 Week 36th Saturday
  20. 聊聊并发(四)——深入分析ConcurrentHashMap

热门文章

  1. winform-windowsmediaplayer设置可视化效果之条形
  2. [转载]GCC 编译使用动态链接库和静态链接库--及先后顺序----及环境变量设置总结
  3. 第二阶段团队冲刺-seven
  4. C++11 tuple元组
  5. 【AtCoder】AGC012
  6. 洛谷P1522 牛的旅行
  7. jQuery遍历文档(重要)
  8. Cocos2d-x中图字原理之深入分析
  9. Linux内核情景分析之异常访问,用户堆栈的扩展
  10. 如何使用python下载网站上的视频