首页 > 代码库 > Object-c Associated Object

Object-c Associated Object

  oc的关联的作用在我看来就是将两个对象关联起来,用的时候通过key和对象把和这个对象关联的对象再取出来(我做的项目就是和UITableView里面的一个属性关联起来了)

举个栗子:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"关联測试" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonTap{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@""
                                                     message:@"How (are) you doing" delegate:self cancelButtonTitle:@"Not bad" otherButtonTitles:@"Fine", nil];
    void (^block)(NSInteger) = ^(NSInteger buttonIndex){
        if (buttonIndex == 0) {
            NSLog(@"buttonIndex:0");
        }else{
            NSLog(@"buttonIndex:1");
        }
    };
    objc_setAssociatedObject(alert, key, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    void (^block)(NSInteger) = objc_getAssociatedObject(alertView, key);
    block(buttonIndex);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

上面的栗子就是把alert和block关联起来,你关联的什么对象类型,取得就是什么对象类型,然后利用取出来的对象做你想做的事,上面的栗子就是取出block。完了在block里面做你想做的事,而且block在alert后面。使你看代码更easy一些。(在我看来。然并卵),这个栗子也算是我从《Effective Object-c》借鉴来的,真心认为这个自己不太好,可是对于理解对象关联够用了。OBJC_ASSOCIATION_RETAIN_NONATOMIC这个和(nonatomic,retain)是一样的作用。例如以下表

OBJC_ASSOCIATION_ASSIGN @property (assign) or @property (unsafe_unretained) Specifies a weak reference to the associated object.
OBJC_ASSOCIATION_RETAIN_NONATOMIC @property (nonatomic, strong) Specifies a strong reference to the associated object, and that the association is not made atomically.
OBJC_ASSOCIATION_COPY_NONATOMIC @property (nonatomic, copy) Specifies that the associated object is copied, and that the association is not made atomically.
OBJC_ASSOCIATION_RETAIN @property (atomic, strong) Specifies a strong reference to the associated object, and that the association is made atomically.
OBJC_ASSOCIATION_COPY @property (atomic, copy) Specifies that the associated object is copied, and that the association is made atomically.
并且。用block还要注意循环引用的问题。这个在这里就不多说了,我个人觉得http://kingscocoa.com/tutorials/associated-objects/这个写的不错,是全英文的。只是不难,建议能够看看,理解问题会更上一层楼,这个博客解说了为什么用关联,举得栗子就是正常写的情况下,记录你要删除的cell的IndexPath。会把这个IndexPath弄成全局变量来记录。完了操作删除,可是要想到一个问题就是我要是在其它地方也操作这个值不就会乱套么,或者我就用一次,用全局变量就太麻烦了,所以索性用关联记录这个值,于是能够模仿我上面的栗子写,只是博主用了三种方法阐述。第一个就是想我上面栗子一样正常写,另外一种就是给NSObject加拓展方法。传入对象和关联的值。第三种就是给UIALertView加类方法,把IndexPath设置成属性,重写setter和getter方法,我觉得博主的想法非常巧妙。希望借鉴,谢谢




參考链接:http://nshipster.com/associated-objects/

                 http://kingscocoa.com/tutorials/associated-objects/

Object-c Associated Object