Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 2.24 KB

4-associated-object.md

File metadata and controls

78 lines (53 loc) · 2.24 KB

关联对象

为什么使用关联对象?

在分类中 @property 并不会自动生成实例变量以及存取方法, 所以 一般使用关联对象为已经存在的类添加「属性」

方法原型

id objc_getAssociatedObject(id object, const void *key);
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);

objc_AssociationPolicy 与修饰符

objc_AssociationPolicy Modifier
OBJC_ASSOCIATION_ASSIGN assign
OBJC_ASSOCIATION_RETAIN_NONATOMIC nonatomic, strong
OBJC_ASSOCIATION_COPY_NONATOMIC nonatomic, copy
OBJC_ASSOCIATION_RETAIN atomic, strong
OBJC_ASSOCIATION_COPY atomic, copy

使用实例

#import <objc/runtime.h> 
     
static void *EOCMyAlertViewKey = "EOCMyAlertViewKey";  
 
- (void)askUserAQuestion {  
    UIAlertView *alert = [[UIAlertViewalloc]  
                             initWithTitle:@"Question"  
                               message:@"What do you want to do?"  
                                  delegate:self  
                        cancelButtonTitle:@"Cancel"  
                        otherButtonTitles:@"Continue", nil];  
 
        void (^block)(NSInteger) = ^(NSInteger buttonIndex){  
          if (buttonIndex == 0) {  
              [self doCancel];  
        } else {  
            [self doContinue];  
        }  
    };  
 
      objc_setAssociatedObject(alert,  
                               EOCMyAlertViewKey,  
                               block,  
O                              BJC_ASSOCIATION_COPY);  
 
      [alert show];  
}  
 
// UIAlertViewDelegate protocol method  
- (void)alertView:(UIAlertView*)alertView  
        clickedButtonAtIndex:(NSInteger)buttonIndex  
{  
    void (^block)(NSInteger) =  
        objc_getAssociatedObject(alertView, EOCMyAlertViewKey);  
    block(buttonIndex);  
} 

参考

  1. iOS底层原理总结 - 关联对象实现原理
  2. 关联对象详解
  3. 关联对象 AssociatedObject 完全解析