通过 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
功能:进行对象之间的关联,需要时,可以获取到关联的对象
四个参数:需要四个参数:源对象,关键字,关联的对象和一个关联策略。
//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
源对象
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
需要头文件 #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
相关文章推荐
- Responder object
- WaitForSingleObject函数
- <转载>Js中Prototype、__proto__、Constructor、Object、Function关系介绍
- Xcode编译时出现NSObjCRuntime,NSZone,NSObject以及could not build module 'Foundation'等等错误
- QObject类 moc处理后代码
- GitHub 上排名前 100 的 Objective-C 项目简介
- Java Object
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- 举例讲解Objective-C中@property属性的用法
- Object-C 之 可变参数
- Objective-C与JavaScript交互的那些事
- pip安装模块警告InsecurePlatformWarning: A true SSLContext object is not available.
- GitHub 上排名前 100 的 Objective-C 项目简介
- Objective-C Runtime 运行时之一:类与对象
- objective-C学习笔记(七) 字符串处理
- runtime(二)
- 对Object类中方法的理解
- Objective-C——retain/copy/assign/atomic/nonatomic/strong/weak/readonly/readwrite详解
- python使用PIL库报错,'NoneType' object has no attribute 'bands'