通过 objc_setAssociatedObject (关联) 的形式实现为Category (类别) 添加属性

先了解一下 objc_setAssociatedObject  objc_getAssociatedObject

需要头文件 #import <objc/runtime.h>

objc_setAssociatedObject



Sets an associated value for a given object using a given key and association policy. 

Declaration

void objc_setAssociatedObject(id objectvoid *keyid valueobjc_AssociationPolicy policy)

Parameters

object

The source object for the association.

key

The key for the association.

value

The value to associate with the key key for object. Pass nil to clear an existing association.

policy

The policy for the association. For possible values, see Associative Object Behaviors.

功能:进行对象之间的关联,需要时,可以获取到关联的对象

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

    //1 object:源对象

    //2 key:进行关联的关键字 唯一静态变量key associatedkey

    //3 value:关联的对象

    //4 policy:关键策略 (

typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {

    OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */

    OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object. 

                                            *   The association is not made atomically. */

    OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied. 

                                            *   The association is not made atomically. */

    OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object.

                                            *   The association is made atomically. */

    OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied.

                                            *   The association is made atomically. */

};

)




objc_getAssociatedObject

Returns the value associated with a given object for a given key. 

Declaration


id objc_getAssociatedObject(id objectvoid *key)

Parameters

object

The source object for the association.

key

The key for the association.


功能:获取关联的对象

两个参数: object 源对象

key 进行关联的关键字


//举个栗子================================================


//UIButton+test.h———

#import <UIKit/UIKit.h>


@interface UIButton (test)

@property(nonatomic,assign)NSString *name;

@end


//UIButton+test.m——-


#import "UIButton+test.h"

#import <objc/runtime.h>

@implementation UIButton (test)


static const char Name;


-(void)setName:(NSString *)name

{

    // 进行绑定,name btn 绑定起来,将来就可以通过 btn 获得 name

    objc_setAssociatedObject(self, &Name,  name, OBJC_ASSOCIATION_COPY_NONATOMIC);


}

-(NSString *)name

{

    // 通过self 关键字 获得绑定的name

   return objc_getAssociatedObject(self, &Name);

}




@end




//TestViewController.h———


#import <UIKit/UIKit.h>


@interface TestViewController : UIViewController


@end


//TestViewController.m———

#import "TestViewController.h"

#import "UIButton+test.h"

@interface TestViewController ()


@end


@implementation TestViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.


    UIButton *but = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];

    but.backgroundColor =[UIColor redColor];

    but.name = @"Brian";

    [but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

    

    

    [self.view addSubview:but];

    

    

    

}

-(void)butClick:(UIButton *)but

{

   NSLog(@"name= %@",but.name);

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值