[置顶] 浅析objc的消息机制

 学习ios的同学都知道ojbc一种runtime的语言,runtime表明函数的真正执行的时候来确定函数执行的。这样的好处就是我们能很灵活的设计我们的代码,也能在看似合法的情况下做一些非常有意思的事情,要了解ios的runtime,我们需要了解ios的类结构,ios所有的类的基类都是NSObject这个类,从这个类来分析ios的runtime机制。

下面我们在xcode 中打开 NSObject 的声明,为了简单明了,我省略了很多,类型和宏的声明。
NS_ROOT_CLASS
@interface NSObject <NSObject> {
    Class	isa;
}

+ (void)load;

+ (void)initialize;
- (id)init;

+ (id)new;
+ (id)allocWithZone:(NSZone *)zone;
+ (id)alloc;
- (void)dealloc;

- (void)finalize;

- (id)copy;
- (id)mutableCopy;

+ (id)copyWithZone:(NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;
+ (id)mutableCopyWithZone:(NSZone *)zone NS_AUTOMATED_REFCOUNT_UNAVAILABLE;

+ (Class)superclass;
+ (Class)class;
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
- (IMP)methodForSelector:(SEL)aSelector;
+ (IMP)instanceMethodForSelector:(SEL)aSelector;
- (void)doesNotRecognizeSelector:(SEL)aSelector;

- (id)forwardingTargetForSelector:(SEL)aSelector NS_AVAILABLE(10_5, 2_0);
- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;

+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector;

- (BOOL)allowsWeakReference NS_UNAVAILABLE;
- (BOOL)retainWeakReference NS_UNAVAILABLE;

+ (NSString *)description;

+ (BOOL)isSubclassOfClass:(Class)aClass;

+ (BOOL)resolveClassMethod:(SEL)sel NS_AVAILABLE(10_5, 2_0);
+ (BOOL)resolveInstanceMethod:(SEL)sel NS_AVAILABLE(10_5, 2_0);

@end

 如您所见,NSObject的对象非常之简单,Class 类型的,我们再看Class 是什么:


#include <sys/types.h>      // for __DARWIN_NULL
#include <Availability.h>
#include <objc/objc-api.h>


typedef struct objc_class *Class;
typedef struct objc_object {
    Class isa;
} *id;


  ok,Class是一个 objc_class 的结构体的指针,现在我们只要了解objc_class的结构就好了。

typedef struct objc_method *Method;
typedef struct objc_ivar *Ivar;
typedef struct objc_category *Category;
typedef struct objc_property *objc_property_t;

struct objc_class {
    Class isa;

#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;

  现在明了了,super_class是其父类的指针,name是类名,version,info,instance_size是表明这个类的信息, ivars是这个类的变量列表,methodLists是函数列表,cache,是一个方法的list的cache,protocols当然是一个protocol的list。
   好了,我们对NSobject 类有了基本的了解了,

[aObjec performSelector:aSEL]; 当这行代码执行的时候,aObject 首先会去找cache,里头有没有对应函数,如果找不到,会去找methodLists 列表中是否含有对应的函数,如果再没有就会去找 super_class 中的cache,然后..... 继续这样的步骤.......

       所以我们现在了解了NSObject 对于消息的发送的基本的机制流程,后续我会介绍NSOject 我们在这个机制上能做的一些事情。











原文地址:https://www.cnblogs.com/suncoolcat/p/3331176.html