关联对象(Associated Object)
- category是object-c的分类,通常用来对类进行扩展,添加方法。如果直接添加属性的话,并不能使用,因为系统只会生成一个声明,并不像在普通类里面,属性会自动生成成员变量和set,get方法。如果要在分类里面添加属性的话,关联对象就派上用场了。
- 关联对象类似于成员变量,不过是在运行时添加的,需要我们手动添加set和get方法。在set和get中分别使用void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)和void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy) 方法。
- 我们还需要指定一个内存管理策略,以告诉Runtime如何管理这个对象的内存。这个内存管理的策略可以由以下值指定:
<pre>
/**
- Policies related to associative references.
- These are options to objc_setAssociatedObject()
/
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. */
};
</pre>
关联对象最佳实践
关联对象理解起来不难,实际使用也很容易,但是不能滥用,以下是一个实际使用的例子。
- People的h文件
<pre>
@interface People : NSObject
{
NSString * _name;
NSString * _reName;
}
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) NSString *country;
/**
- 初始化
- @param reName
- @param reName
- @return
*/
- (instancetype)initWithName:(NSString )name reName:(NSString )reName;
/
- 获取所有的成员变量
- @return
*/
- (NSDictionary )allIvar;
/*
- 获取所有的
- @return return value description
*/
- (NSDictionary *)allProperty;
@end
</pre>
- People类的分类,添加了company和position两个关联对象
<pre>
@interface People (BTExtend)
//在分类里直接添加属性的话,并不会生成成员变量和set以及get方法
@property (nonatomic, copy) NSString *company;
@property (nonatomic, strong) NSString *position;
@end
@implementation People (BTExtend)
(NSString *)company
{
return objc_getAssociatedObject(self, _cmd);
}(void)setCompany:(NSString *)company
{
objc_setAssociatedObject(self, @selector(company), company, OBJC_ASSOCIATION_COPY_NONATOMIC);
}(NSString *)position
{
return objc_getAssociatedObject(self, _cmd);
}(void)setPosition:(NSString *)position
{
objc_setAssociatedObject(self, @selector(position), position, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
</pre>遍历People实例对象的成员变量和属性
<pre>
void ivarAndPropertyList()
{
People *people = [[People alloc] initWithName:@"大傻" reName:@"二傻"];
people.age = 18;
people.country = @"japan";
people.company = @"tv";
people.position = @"av";
NSDictionary *ivarList = [people allIvar];
NSDictionary *propertyList = [people allProperty];
NSLog(@"ivarList:%@\npropertyList:%@",ivarList,propertyList);
}
//运行结果
2016-06-06 22:22:49.271 runtimeDemo[1368:49233] ivarList:{
** "_age" = 18;**
** "_country" = japan;**
** "_name" = "\U5927\U50bb";**
** "_reName" = "\U4e8c\U50bb";**
}
propertyList:{
** age = 18;**
** company = tv;**
** country = japan;**
** position = av;**
}
</pre>