runtime实现原理

1.runtime实现的机制是什么?

runtime是一套比较底层的纯C语言API, 属于一个C语言库, 包含了很多底层的C语言API。 
在我们平时编写的OC代码中, 程序运行过程时, 其实最终都是转成了runtime的C语言代码, runtime算是OC的幕后工作者!

例如,下面一个创建Dog对象的方法中, 
OC : 
[[Dog alloc] init] 
runtime : 
objc_msgSend(objc_msgSend(“Dog” , “alloc”), “init”)

2.runtime 用来干什么的? 
runtime是属于OC的底层, 可以进行一些非常底层的操作(用OC是无法现实的, 不好实现)

  • 在程序运行过程中, 动态创建一个类(比如KVO的底层实现)

  • 在程序运行过程中, 动态地为某个类添加属性\方法, 修改属性值\方法

  • 遍历一个类的所有成员变量(属性)\所有方法 
    例如:我们需要对一个类的属性进行归档解档的时候属性特别的多,这时候,我们就会写很多对应的代码,但是如果使用了runtime就可以动态设置! 

学习,runtime机制首先要了解下面几个问题 
1相关的头文件和函数 
1> 头文件


  • 利用头文件,我们可以查看到runtime中的各个方法! 

2> 相关应用

  • NSCoding(归档和解档, 利用runtime遍历模型对象的所有属性)
  • 字典 –> 模型 (利用runtime遍历模型对象的所有属性, 根据属性名从字典中取出对应的值, 设置到模型的属性上)
  • KVO(利用runtime动态产生一个类)
  • 用于封装框架(想怎么改就怎么改) 
    这就是我们runtime机制的只要运用方向

3> 相关函数

  • objc_msgSend : 给对象发送消息
  • class_copyMethodList : 遍历某个类所有的方法
  • class_copyIvarList : 遍历某个类所有的成员变量
  • class_….. 
    这是我们学习runtime必须知道的函数!

4.必备常识 
1> Ivar : 成员变量 
2> Method : 成员方法 



Runtim中的一些方法与属性:(来自头文件)

#if !OBJC_TYPES_DEFINED


/// An opaque type that represents a method in a class definition.

typedef struct objc_method *Method;


/// An opaque type that represents an instance variable.

typedef struct objc_ivar *Ivar;


/// An opaque type that represents a category.

typedef structobjc_category *Category;


/// An opaque type that represents an Objective-C declared property.

typedef structobjc_property *objc_property_t;


在Objective-C的Runtime中有个类型是Class(只在Runtime环境中使用),用来表示Objective-C中的类,其定义为:

typedef struct objc_class *Class;
可以看出,其实Class类型是一个指针,指向struct  objc_class,而struct  objc_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 *` */


#endif


#ifdef __OBJC__

@class Protocol;

#else

typedef struct objc_object Protocol;

#endif


/// Defines a method

struct objc_method_description {

SEL name;              /**< The name of the method */

char *types;           /**< The types of the method arguments */

};


/// Defines a property attribute

typedef struct {

    constchar *name;          /**< The name of the attribute */

    constchar *value;         /**< The value of the attribute (usually empty) */

} objc_property_attribute_t;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值