两对象的关联

  1. 1.基本资料
  2. //policy  
  3. enum {  
  4.     OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */  
  5.     OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1/**< Specifies a strong reference to the associated object. 
  6.                                             *   The association is not made atomically. */  
  7.     OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 
  8.                                             *   The association is not made atomically. */  
  9.     OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object. 
  10.                                             *   The association is made atomically. */  
  11.     OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied. 
  12.                                             *   The association is made atomically. */  
  13. };  
  14.   
  15. /// Type to specify the behavior of an association.  
  16. typedef uintptr_t objc_AssociationPolicy;  
  17.   
  18.   
  19.   
  20.   
  21.   
  22. //method  
  23. void objc_setAssociatedObject(id object, const voidvoid *key, id value, objc_AssociationPolicy policy)  
  24.   
  25. id objc_getAssociatedObject(id object, const voidvoid *key)  
  26.   
  27. void objc_removeAssociatedObjects(id object)  


关联的方式
OBJC_ASSOCIATION_ASSIGN 弱引用
OBJC_ASSOCIATION_RETAIN_NONATOMIC 强类型引用并原子安全性
OBJC_ASSOCIATION_COPY_NONATOMIC copy内容并创建新地址
OBJC_ASSOCIATION_RETAIN 
OBJC_ASSOCIATION_COPY

2.基本概念
把一个value关联到另外一个object里,类似NSDictionary的 setValue:forKey 。
用 objc_setAssociatedObject 关联以后,用 objc_getAssociatedObject 取出使用。
objc_AssociationPolicy  属性 是设定该value在object内的属性,即 assgin, (retain,nonatomic)...等

3.例:
  1. <pre name="code" class="objc">  
  2. #import <objc/runtime.h>   
  3.   
  4. -(void)testAlertBlock  
  5. {  
  6.     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"this is a bolck_alert"  
  7.                                                   message:@"testtesttest"  
  8.                                                  delegate:self  
  9.                                         cancelButtonTitle:@"取消"  
  10.                                         otherButtonTitles:@"确定", nil nil];  
  11.      
  12.     void (^block)()=^{  
  13.         NSLog(@"alert pressed sure-button");  
  14.     };  
  15.     objc_setAssociatedObject(alert, @"Alert_block", block, OBJC_ASSOCIATION_COPY_NONATOMIC);  
  16.     [alert show];  
  17. }  
  18.   
  19. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  20. {  
  21.     void (^block)()=objc_getAssociatedObject(alertView, @"Alert_block");  
  22.     if (block) {  
  23.         block();  
  24.     }  
  25. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值