ocRuntime基本功


//所以类都继承于该类
//任何拥有isa指针都可以称为对象
NSObject {
Class _Nonnull isa ; // 结构体指针
}


// 因此 objc_class 也是对象 ,这也就是为什么说类也是对象,具体参加《深入浅出 Cocoa 教程》
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
Class _Nullable super_class //父类
const char * _Nonnull name //类名
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list * _Nullable ivars //变量列表
struct objc_method_list * _Nullable * _Nullable methodLists //方法列表
struct objc_cache * _Nonnull cache //缓存 可以直接无视
struct objc_protocol_list * _Nullable protocols //协议列表 具体遵守哪些协议
#endif

} OBJC2_UNAVAILABLE;

有了这些 我们就可以做很多事情
1. 动态添加属性、修改属性
2. 动态添加方法,修改方法
3. 获取对象属性列表
4. 查询某类是否遵循某一协议
5. 查询某类是否可以执行某一个具体的方法
6. 动态查找其父类 和 静态类

原文地址:https://www.cnblogs.com/TengSys/p/8227683.html