iOS Kit

iOS面试题—runtime相关

2021-03-28  本文已影响0人  雾中探雪

一、数据结构:objc_object,objc_class,isa,class_data_bits_t,cache_t,method_t

 struct cache_t {
    struct bucket_t *_buckets;//一个散列表,用来方法缓存,bucket_t类型,包含key以及方法实现IMP
    mask_t _mask;//分配用来缓存bucket的总数
    mask_t _occupied;//表明目前实际占用的缓存bucket的个数
}
struct bucket_t {
    private:
    cache_key_t _key;
    IMP _imp;
 }
struct class_rw_t {
     uint32_t flags;
     uint32_t version;
     const class_ro_t *ro;

     method_array_t methods;
     property_array_t properties;
     protocol_array_t protocols;

     Class firstSubclass;
     Class nextSiblingClass;

     char *demangledName;
}    

Objc的类的属性、方法、以及遵循的协议都放在class_rw_t中,class_rw_t代表了类相关的读写信息,是对class_ro_t的封装,而class_ro_t代表了类的只读信息,存储了 编译器决定了的属性、方法和遵守协议

struct class_ro_t {
    uint32_t flags;
    uint32_t instanceStart;
    uint32_t instanceSize;
    #ifdef __LP64__
    uint32_t reserved;
    #endif

    const uint8_t * ivarLayout;

    const char * name;
    method_list_t * baseMethodList;
    protocol_list_t * baseProtocols;
    const ivar_list_t * ivars;

    const uint8_t * weakIvarLayout;
    property_list_t *baseProperties;

    method_list_t *baseMethods() const {
    return baseMethodList;
    }
  };
struct method_t {
  SEL name;           //名称
  const char *types;//返回值和参数
  IMP imp;              //函数体
}

二、 对象,类对象,元类对象

三、消息传递

   void objc_msgSend(void /* id self, SEL op, ... */ )
   void objc_msgSendSuper(void /* struct objc_super *super, SEL op, ... */ )
   struct objc_super {
       /// Specifies an instance of a class.
       __unsafe_unretained _Nonnull id receiver;
       /// Specifies the particular superclass of the instance to message. 
   #if !defined(__cplusplus)  &&  !__OBJC2__
       /* For compatibility with old objc-runtime.h header */
       __unsafe_unretained _Nonnull Class class;
   #else
       __unsafe_unretained _Nonnull Class super_class;
   #endif
       /* super_class is the first class to search */
   };

消息传递的流程:缓存查找-->当前类查找-->父类逐级查找

四、消息转发

+ (BOOL)resolveInstanceMethod:(SEL)sel;//为对象方法进行决议
+ (BOOL)resolveClassMethod:(SEL)sel;//为类方法进行决议
- (id)forwardingTargetForSelector:(SEL)aSelector;//方法转发目标
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
- (void)forwardInvocation:(NSInvocation *)anInvocation;

那么最后消息未能处理的时候,还会调用到
-(void)doesNotRecognizeSelector:(SEL)aSelector`这个方法,我们也可以在这个方法中做处理,避免掉crash,但是只建议在线上环境的时候做处理,实际开发过程中还要把异常抛出来

     + (void)load
   {
       Method test = class_getInstanceMethod(self, @selector(test));
       Method otherTest = class_getInstanceMethod(self, @selector(otherTest));
       method_exchangeImplementations(test, otherTest);
   }

应用场景:替换系统的方法,比如viewDidLoad,viewWillAppear以及一些响应方法,来进行统计信息

    class_addMethod(self, sel, testImp, "v@:");
    void testImp (void)
    {
        NSLog(@"testImp");
    }
上一篇下一篇

猜你喜欢

热点阅读