runtime一些事儿

交换方法

Method method1 = class_getInstanceMethod([classTwo class], @selector(methodTwo));
Method method2 = class_getInstanceMethod([classOne class], @selector(methodOne));
method_exchangeImplementations(method1, method2);

通过runtime动态关联对象

主要用到了objc_setAssociatedObject,objc_getAssociatedObject以及objc_removeAssociatedObjects

//在目标target上添加关联对象,属性名propertyname(也能用来添加block),值value  
+ (void)addAssociatedWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  
    id property = objc_getAssociatedObject(target, &propertyName);  

    if(property == nil)  
    {  
        property = value;  
        objc_setAssociatedObject(target, &propertyName, property, OBJC_ASSOCIATION_RETAIN);  
    }  
}  

//获取目标target的指定关联对象值  
+ (id)getAssociatedValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
    id property = objc_getAssociatedObject(target, &propertyName);  
    return property;  
}  

优点:这种方式能够使我们快速的在一个已有的class内部添加一个动态属性或block块。
缺点:不能像遍历属性一样的遍历我们所有关联对象,且不能移除制定的关联对象,只能通过removeAssociatedObjects方法移除所有关联对象。

通过runtime动态添加Ivar

主要用到objc_allocateClassPair,class_addIvar,objc_registerClassPair

//在目标target上添加属性(已经存在的类不支持,可跳进去看注释),属性名propertyname,值value  
+ (void)addIvarWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  
    if (class_addIvar([target class], [propertyName UTF8String], sizeof(id), log2(sizeof(id)), "@")) {  
        YYLog(@"创建属性Ivar成功");  
    }  
}  

//获取目标target的指定属性值  
+ (id)getIvarValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
    Ivar ivar = class_getInstanceVariable([target class], [propertyName UTF8String]);  
    if (ivar) {  
        id value = object_getIvar(target, ivar);  
        return value;  
    } else {  
        return nil;  
    }  
}  

优点:动态添加Ivar我们能够通过遍历Ivar得到我们所添加的属性。
缺点:不能在已存在的class中添加Ivar,所有说必须通过objc_allocateClassPair动态创建一个class,才能调用class_addIvar创建Ivar,最后通过objc_registerClassPair注册class。

通过runtime动态添加property

主要用到class_addProperty,class_addMethod,class_replaceProperty,class_getInstanceVariable



//在目标target上添加属性,属性名propertyname,值value  
+ (void)addPropertyWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  

    //先判断有没有这个属性,没有就添加,有就直接赋值  
    Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String]);  
    if (ivar) {  
        return;  
    }  

    /* 
     objc_property_attribute_t type = { "T", "@\"NSString\"" }; 
     objc_property_attribute_t ownership = { "C", "" }; // C = copy 
     objc_property_attribute_t backingivar  = { "V", "_privateName" }; 
     objc_property_attribute_t attrs[] = { type, ownership, backingivar }; 
     class_addProperty([SomeClass class], "name", attrs, 3); 
     */  

    //objc_property_attribute_t所代表的意思可以调用getPropertyNameList打印,大概就能猜出  
    objc_property_attribute_t type = { "T", [[NSString stringWithFormat:@"@\"%@\"",NSStringFromClass([value class])] UTF8String] };  
    objc_property_attribute_t ownership = { "&", "N" };  
    objc_property_attribute_t backingivar  = { "V", [[NSString stringWithFormat:@"_%@", propertyName] UTF8String] };  
    objc_property_attribute_t attrs[] = { type, ownership, backingivar };  
    if (class_addProperty([target class], [propertyName UTF8String], attrs, 3)) {  

        //添加get和set方法  
        class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter, "@@:");  
        class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter, "v@:@");  

        //赋值  
        [target setValue:value forKey:propertyName];  
        NSLog(@"%@", [target valueForKey:propertyName]);  

        YYLog(@"创建属性Property成功");  
    } else {  
        class_replaceProperty([target class], [propertyName UTF8String], attrs, 3);  
        //添加get和set方法  
        class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter, "@@:");  
        class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter, "v@:@");  

        //赋值  
        [target setValue:value forKey:propertyName];  
    }  
}  

id getter(id self1, SEL _cmd1) {  
    NSString *key = NSStringFromSelector(_cmd1);  
    Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
    NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar);  
    return [dictCustomerProperty objectForKey:key];  
}  

void setter(id self1, SEL _cmd1, id newValue) {  
    //移除set  
    NSString *key = [NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];  
    //首字母小写  
    NSString *head = [key substringWithRange:NSMakeRange(0, 1)];  
    head = [head lowercaseString];  
    key = [key stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head];  
    //移除后缀 ":"  
    key = [key stringByReplacingCharactersInRange:NSMakeRange(key.length - 1, 1) withString:@""];  

    Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
    NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar);  
    if (!dictCustomerProperty) {  
        dictCustomerProperty = [NSMutableDictionary dictionary];  
        object_setIvar(self1, ivar, dictCustomerProperty);  
    }  
    [dictCustomerProperty setObject:newValue forKey:key];  
}  

+ (id)getPropertyValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
    //先判断有没有这个属性,没有就添加,有就直接赋值  
    Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String]);  
    if (ivar) {  
        return object_getIvar(target, ivar);  
    }  

    ivar = class_getInstanceVariable([target class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
    NSMutableDictionary *dict = object_getIvar(target, ivar);  
    if (dict && [dict objectForKey:propertyName]) {  
        return [dict objectForKey:propertyName];  
    } else {  
        return nil;  
    }  
} 

优点:这种方法能够在已有的类中添加property,且能够遍历到动态添加的属性。
缺点:比较麻烦,getter和setter需要自己写,且值也需要自己存储,如上面的代码,我是把setter中的值存储到了_dictCustomerProperty里面,在getter中再从_dictCustomerProperty读出值。

通过setValue:forUndefinedKey动态添加键值

这种方法优点类似property,需要重写setValue:forUndefinedKey和valueForUndefinedKey:,存值方式也一样,需要借助一个其它对象。由于这种方式没通过runtime,所以也比较容易理解。在此就不举例了。

你会被问到的关于runtime笔试题。

  1. runtime怎么添加属性、方法等
  2. runtime 如何实现 weak 属性
  3. runtime如何通过selector找到对应的IMP地址?(分别考虑类方法和实例方法)
  4. 使用runtime Associate方法关联的对象,需要在主对象dealloc的时候释放么?
  5. _objc_msgForward函数是做什么的?直接调用它将会发生什么?
  6. 能否向编译后得到的类中增加实例变量?能否向运行时创建的类中添加实例变量?为什么?
  7. 简述下Objective-C中调用方法的过程(runtime)
  8. 什么是method swizzling(俗称黑魔法)。

解答

runtime怎么添加属性、方法等
ivar表示成员变量
class_addIvar
class_addMethod
class_addProperty
class_addProtocol
class_replaceProperty
runtime 如何实现 weak 属性

首先要搞清楚weak属性的特点
weak策略表明该属性定义了一种“非拥有关系” (nonowning relationship)。为这种属性设置新值时,设置方法既不保留新值,也不释放旧值。此特质同assign类似;然而在属性所指的对象遭到摧毁时,属性值也会清空(nil out)

那么runtime如何实现weak变量的自动置nil?

runtime对注册的类,会进行布局,会将 weak 对象放入一个 hash 表中。用 weak 指向的对象内存地址作为 key,当此对象的引用计数为0的时候会调用对象的 dealloc 方法,假设 weak 指向的对象内存地址是a,那么就会以a为key,在这个 weak hash表中搜索,找到所有以a为key的 weak 对象,从而设置为 nil。

weak属性需要在dealloc中置nil么
在ARC环境无论是强指针还是弱指针都无需在 dealloc 设置为 nil , ARC 会自动帮我们处理
即便是编译器不帮我们做这些,weak也不需要在dealloc中置nil
在属性所指的对象遭到摧毁时,属性值也会清空

objc// 模拟下weak的setter方法,大致如下- (void)setObject:(NSObject *)object{ objc_setAssociatedObject(self, “object”, object, OBJC_ASSOCIATION_ASSIGN); [object cyl_runAtDealloc:^{ _object = nil; }];}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值