ios動態添加屬性的幾種方法


http://blog.csdn.net/shengyumojian/article/details/44919695

在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 self1, SEL _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 self1, SEL _cmd1, id newValue) {  
  53.     //移除set  
  54.     NSString *key = [NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];  
  55.     //首字母小寫  
  56.     NSString *head = [key substringWithRange:NSMakeRange(0, 1)];  
  57.     head = [head lowercaseString];  
  58.     key = [key stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head];  
  59.     //移除后綴 ":"  
  60.     key = [key stringByReplacingCharactersInRange:NSMakeRange(key.length - 1, 1) 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,所以也比較容易理解。在此就不舉例了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM