关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)

 关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。
 关联特性只有在Mac OS X V10.6以及以后的版本上才是可用的。

在类的定义之外为类增加额外的存储空间

    使用关联,我们可以不用修改类的定义而为其对象增加存储空间。这在我们无法访问到类的源码的时候或者是考虑到二进制兼容性的时候是非常有用。
    关联是基于关键字的,因此,我们可以为任何对象增加任意多的关联,每个都使用不同的关键字即可。关联是可以保证被关联的对象在关联对象的整个生命周期都是可用的(在垃圾自动回收环境下也不会导致资源不可回收)

创建关联

    创建关联要使用到Objective-C的运行时函数:objc_setAssociatedObject来把一个对象与另外一个对象进行关联。该函数需要四个参数:源对象,关键字,关联的对象和一个关联策略。当然,此处的关键字和关联策略是需要进一步讨论的。
  ■  关键字是一个void类型的指针。每一个关联的关键字必须是唯一的。通常都是会采用静态变量来作为关键字。
  ■  关联策略表明了相关的对象是通过赋值,保留引用还是复制的方式进行关联的;还有这种关联是原子的还是非原子的。这里的关联策略和声明属性时的很类似。这种关联策略是通过使用预先定义好的常量来表示的。

断开关联

    断开关联是使用objc_setAssociatedObject函数,传入nil值即可。
    接着列表7-1中的程序,我们可以使用如下的代码来断开字符串overview和arry之间的关联:

         objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN);  

   其中,被关联的对象为nil,此时关联策略也就无关紧要了。
    使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。

  1. #import <Foundation/Foundation.h>  
  2. #import <objc/runtime.h>  
  3.   
  4.   
  5. int main(int argc, const char* argv[])  
  6. {  
  7.     NSAutoreleasePool * pool = [[NSAutoreleasePool] alloc init];  
  8.       
  9.     static char overviewKey;  
  10.     NSArray *array =[[NSArray alloc] initWidthObjects:@"One", @"Two", @"Three", nil];  
  11.     //为了演示的目的,这里使用initWithFormat:来确保字符串可以被销毁  
  12.     NSString * overview = [[NSString alloc] initWithFormat:@"@",@"First three numbers"];  
  13.     objc_setAssociatedObject(array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN);  
  14.     [overview release];  
  15.       
  16.     NSString *associatedObject = (NSString *)objc_getAssociatedObject(arrray, &overviewKey);  
  17.     NSLog(@"associatedObject:%@", associatedObject);  
  18.       
  19.     objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN);  
  20.     [array release];  
  21.       
  22.     [pool drain];  
  23.     return 0;  
  24. }  

例子2:

//唯一静态变量key

static const char associatedkey;

static const char associatedButtonkey;

- (IBAction)sendAlert:(id)sender

{

    NSString *message =@"我知道你是按钮了";

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"我要传值·" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];

    alert.delegate =self;

    [alert show];

     //objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。

     //1 源对象alert

    //2 关键字 唯一静态变量key associatedkey

    //3 关联的对象 sender

    //4 关键策略  OBJC_ASSOCIATION_RETAIN_NONATOMIC    

    objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);    

    objc_setAssociatedObject(alert, &associatedButtonkey, sender,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //通过 objc_getAssociatedObject获取关联对象

    NSString  *messageString =objc_getAssociatedObject(alertView, &associatedkey);

    UIButton *sender = objc_getAssociatedObject(alertView, &associatedButtonkey);

       _labebutton.text = [[sendertitleLabel]text];

    _ThisLabel.text = messageString; 

    //使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。

}

例子3:关联 切记不可滥用

#import <objc/runtime.h>

static const NSString* KAlertButtonKey = @"alertButtonKey";

- (void)showAlert

{

   UIAlertView *alert = [[UIAlertView alloc]

                          initWithTitle:@"warn"

                          message:@"what can I do for you?"

                          delegate:self

                          cancelButtonTitle:@"cancel"

                          otherButtonTitles:@"ok", nil];

    [alert show];

    void (^block)(NSInteger) = ^(NSInteger buttonIndex){

        if (buttonIndex == alert.cancelButtonIndex) {

            NSLog(@"click cancle buttton");

        }

        else{

            NSLog(@"click ok button");

        }

    };

    objc_setAssociatedObject(alert, &KAlertButtonKey, block, OBJC_ASSOCIATION_COPY);

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    void (^block)(NSInteger) = objc_getAssociatedObject(alertView, &KAlertButtonKey);

    block(buttonIndex);

}

posted @ 2015-04-02 10:49  燕羽天空  Views(267)  Comments(0Edit  收藏  举报