iOS动态关联对象

来源:互联网 发布:劫单挑亚索 知乎 编辑:程序博客网 时间:2024/05/17 21:38

在iOS运行过程中,有几种方式能够动态的添加属性。


1-通过runtime动态关联对象


主要用到了objc_setAssociatedObject,objc_getAssociatedObject以及objc_removeAssociatedObjects

[objc] view plain copy
  1. //在目标target上添加关联对象,属性名propertyname(也能用来添加block),值value  
  2. + (void)addAssociatedWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  
  3.     id property = objc_getAssociatedObject(target, &propertyName);  
  4.       
  5.     if(property == nil)  
  6.     {  
  7.         property = value;  
  8.         objc_setAssociatedObject(target, &propertyName, property, OBJC_ASSOCIATION_RETAIN);  
  9.     }  
  10. }  
  11.   
  12. //获取目标target的指定关联对象值  
  13. + (id)getAssociatedValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
  14.     id property = objc_getAssociatedObject(target, &propertyName);  
  15.     return property;  
  16. }  

优点:这种方式能够使我们快速的在一个已有的class内部添加一个动态属性或block块。

缺点:不能像遍历属性一样的遍历我们所有关联对象,且不能移除制定的关联对象,只能通过removeAssociatedObjects方法移除所有关联对象。


2-通过runtime动态添加Ivar


主要用到objc_allocateClassPair,class_addIvar,objc_registerClassPair

[objc] view plain copy
  1. //在目标target上添加属性(已经存在的类不支持,可跳进去看注释),属性名propertyname,值value  
  2. + (void)addIvarWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  
  3.     if (class_addIvar([target class], [propertyName UTF8String], sizeof(id), log2(sizeof(id)), "@")) {  
  4.         YYLog(@"创建属性Ivar成功");  
  5.     }  
  6. }  
  7.   
  8. //获取目标target的指定属性值  
  9. + (id)getIvarValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
  10.     Ivar ivar = class_getInstanceVariable([target class], [propertyName UTF8String]);  
  11.     if (ivar) {  
  12.         id value = object_getIvar(target, ivar);  
  13.         return value;  
  14.     } else {  
  15.         return nil;  
  16.     }  
  17. }  

优点:动态添加Ivar我们能够通过遍历Ivar得到我们所添加的属性。

缺点:不能在已存在的class中添加Ivar,所有说必须通过objc_allocateClassPair动态创建一个class,才能调用class_addIvar创建Ivar,最后通过objc_registerClassPair注册class。


3-通过runtime动态添加property


主要用到class_addProperty,class_addMethod,class_replaceProperty,class_getInstanceVariable

[objc] view plain copy
  1. //在目标target上添加属性,属性名propertyname,值value  
  2. + (void)addPropertyWithtarget:(id)target withPropertyName:(NSString *)propertyName withValue:(id)value {  
  3.       
  4.     //先判断有没有这个属性,没有就添加,有就直接赋值  
  5.     Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String]);  
  6.     if (ivar) {  
  7.         return;  
  8.     }  
  9.       
  10.     /* 
  11.      objc_property_attribute_t type = { "T", "@\"NSString\"" }; 
  12.      objc_property_attribute_t ownership = { "C", "" }; // C = copy 
  13.      objc_property_attribute_t backingivar  = { "V", "_privateName" }; 
  14.      objc_property_attribute_t attrs[] = { type, ownership, backingivar }; 
  15.      class_addProperty([SomeClass class], "name", attrs, 3); 
  16.      */  
  17.       
  18.     //objc_property_attribute_t所代表的意思可以调用getPropertyNameList打印,大概就能猜出  
  19.     objc_property_attribute_t type = { "T", [[NSString stringWithFormat:@"@\"%@\"",NSStringFromClass([value class])] UTF8String] };  
  20.     objc_property_attribute_t ownership = { "&""N" };  
  21.     objc_property_attribute_t backingivar  = { "V", [[NSString stringWithFormat:@"_%@", propertyName] UTF8String] };  
  22.     objc_property_attribute_t attrs[] = { type, ownership, backingivar };  
  23.     if (class_addProperty([target class], [propertyName UTF8String], attrs, 3)) {  
  24.           
  25.         //添加get和set方法  
  26.         class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter"@@:");  
  27.         class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter"v@:@");  
  28.           
  29.         //赋值  
  30.         [target setValue:value forKey:propertyName];  
  31.         NSLog(@"%@", [target valueForKey:propertyName]);  
  32.           
  33.         YYLog(@"创建属性Property成功");  
  34.     } else {  
  35.         class_replaceProperty([target class], [propertyName UTF8String], attrs, 3);  
  36.         //添加get和set方法  
  37.         class_addMethod([target class], NSSelectorFromString(propertyName), (IMP)getter"@@:");  
  38.         class_addMethod([target class], NSSelectorFromString([NSString stringWithFormat:@"set%@:",[propertyName capitalizedString]]), (IMP)setter"v@:@");  
  39.           
  40.         //赋值  
  41.         [target setValue:value forKey:propertyName];  
  42.     }  
  43. }  
  44.   
  45. id getter(id self1SEL _cmd1) {  
  46.     NSString *key = NSStringFromSelector(_cmd1);  
  47.     Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
  48.     NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar);  
  49.     return [dictCustomerProperty objectForKey:key];  
  50. }  
  51.   
  52. void setter(id self1SEL _cmd1id newValue) {  
  53.     //移除set  
  54.     NSString *key = [NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(03) withString:@""];  
  55.     //首字母小写  
  56.     NSString *head = [key substringWithRange:NSMakeRange(01)];  
  57.     head = [head lowercaseString];  
  58.     key = [key stringByReplacingCharactersInRange:NSMakeRange(01) withString:head];  
  59.     //移除后缀 ":"  
  60.     key = [key stringByReplacingCharactersInRange:NSMakeRange(key.length - 11) withString:@""];  
  61.       
  62.     Ivar ivar = class_getInstanceVariable([self1 class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
  63.     NSMutableDictionary *dictCustomerProperty = object_getIvar(self1, ivar);  
  64.     if (!dictCustomerProperty) {  
  65.         dictCustomerProperty = [NSMutableDictionary dictionary];  
  66.         object_setIvar(self1, ivar, dictCustomerProperty);  
  67.     }  
  68.     [dictCustomerProperty setObject:newValue forKey:key];  
  69. }  
  70.   
  71. + (id)getPropertyValueWithTarget:(id)target withPropertyName:(NSString *)propertyName {  
  72.     //先判断有没有这个属性,没有就添加,有就直接赋值  
  73.     Ivar ivar = class_getInstanceVariable([target class], [[NSString stringWithFormat:@"_%@", propertyName] UTF8String]);  
  74.     if (ivar) {  
  75.         return object_getIvar(target, ivar);  
  76.     }  
  77.       
  78.     ivar = class_getInstanceVariable([target class], "_dictCustomerProperty");  //basicsViewController里面有个_dictCustomerProperty属性  
  79.     NSMutableDictionary *dict = object_getIvar(target, ivar);  
  80.     if (dict && [dict objectForKey:propertyName]) {  
  81.         return [dict objectForKey:propertyName];  
  82.     } else {  
  83.         return nil;  
  84.     }  
  85. }  

优点:这种方法能够在已有的类中添加property,且能够遍历到动态添加的属性。

缺点:比较麻烦,getter和setter需要自己写,且值也需要自己存储,如上面的代码,我是把setter中的值存储到了_dictCustomerProperty里面,在getter中再从_dictCustomerProperty读出值。


4-通过setValue:forUndefinedKey动态添加键值


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

原创粉丝点击