IOS Runtime-初识runtime(一)

苹果公布了runtime的源码,可以从地址下载:http://www.opensource.apple.com/tarballs/objc4/

object-c程序在启动的时候,需要一个准备时间,这个时间是对cache的初始化,之后所有的消息发送先访问cache。

1.先看看objc.h文件

#if !OBJC_TYPES_DEFINED
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;

/// Represents an instance of a class.
struct objc_object {
    Class isa  OBJC_ISA_AVAILABILITY;
};

/// A pointer to an instance of a class.
typedef struct objc_object *id;
#endif

/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;

/// A pointer to the function of a method implementation. 
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ ); 
#else
typedef id (*IMP)(id, SEL, ...); 
#endif

   我们看到,其实Class是一个结构体(struct)定义的,所以说Class是一个结构体。id是结构体的一个指针。id可以是任何一种对象。

   程序运行时,我们向Object发送消息时,Runtime会根据Object的isa指针找到把Object实例化的类,然后找到之后在这个类中寻找相应的方法或者父类中的方法去运行。

   SEL是指向objc_selector的一个指针,表示方法的名字/签名。

   IMP是一个函数指针,IMP是消息最终调用的执行代码,是方法真正的实现代码。

  

2.再看看runtime.h

   其中定义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 *` */

   分别保存了Class的名字、版本、信息、实例变量大小、成员变量、方法列表、方法缓存、协议。

   可以看到定义Class里有个变量名教super_class,顾名思义是此类的父类,当我们定义继承时,这里就形成了一个继承的闭环,一直到超类为止:

3.便利的方法

    class   返回对象的类;

    isKindOfClass 和 isMemberOfClass检查对象是否在指定的类继承体系中;

    respondsToSelector 检查对象能否相应指定的消息;

    conformsToProtocol 检查对象是否实现了指定协议类的方法;

    methodForSelector  返回指定方法实现的地址。

    performSelector:withObject 执行SEL 所指代的方法。

原文地址:https://www.cnblogs.com/alunchen/p/5364058.html