您的位置:首页 > 移动开发 > Objective-C

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

2016-03-25 17:01 519 查看
先了解一下 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 object, void *key, id value, objc_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 object, void *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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: