介绍

关联对象(AssociatedObject)是Objective-C 2.0运行时的一个特性,允许开发者对已经存在的类在扩展中添加自定义的属性。在实际生产过程中,比较常用的方式是给分类(Category)添加成员变量。

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#import <objc/runtime.h>

@interface NSObject (AssociatedObject)

@property (nonatomic, strong) id property;

@end

@implementation NSObject (AssociatedObject)
@dynamic property;

- (id)property {
return objc_getAssociatedObject(self, _cmd);
}

- (void)setProperty:(NSString *)property {
objc_setAssociatedObject(self, @selector(property), property, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

通过实现代码可以稍微分析下,objc_getAssociatedObject 拿着不变的指针地址(示例传入selector作为参数,实际是void*),从实例中获取需要的对象。objc_setAssociatedObject 根据传入的参数协议,保存指定的对象。

参数协议

1
2
3
4
5
6
7
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_ASSOCIATION_ASSIGN 相当于一个 weak reference,但其实等于 assign/unsafe_unretained

对于与weak的区别不在本文讨论范围内,浅显的区别在于变量释放后,weak 会把引用置空,unsafe_unretained会保留内存地址,一旦获取可能会野指针闪退。

总结

我们知道,如果类要添加变量,只有在objc_allocateClassPairobjc_registerClassPair之间addIvar。等类注册后,变量结构就不允许再被改变,这是为了防止两个相同类的实例拥有不同变量导致运行困惑。

那么在runtime时给实例添加变量,又不改变类内部变量结构,关联对象就是一个比较好的做法。


关联对象的实现

外部方法

1
2
3
4
5
6
7
8
//Sets an associated value for a given object using a given key and association policy.
void objc_setAssociatedObject(id object, const void * key, id value, objc_AssociationPolicy policy);

//Returns the value associated with a given object for a given key.
id objc_getAssociatedObject(id object, const void * key);

//Removes all associations for a given object.
void objc_removeAssociatedObjects(id object);

相比刚刚例子中的用法,多了一个objc_removeAssociatedObjects,那么可不可以用这个方法来删除不用的关联对象呢?

苹果的文档中解释说这个方法主要用来还原对象到类初始的状态,会移除所有的关联,包括其他模块添加的,因此应该用 objc_setAssociatedObject(..,nil,..) 的方式去卸载。


Setter实现

objc_setAssociatedObject实际调用的是_object_set_associative_reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void _object_set_associative_reference(id object, void *key, id value, uintptr_t policy) {
// retain the new value (if any) outside the lock.
ObjcAssociation old_association(0, nil);
id new_value = value ? acquireValue(value, policy) : nil;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
// break any existing association.
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// secondary table exists
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
j->second = ObjcAssociation(policy, new_value);
} else {
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
// create the new association (first time).
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
(*refs)[key] = ObjcAssociation(policy, new_value);
object->setHasAssociatedObjects();
}
} else {
// setting the association to nil breaks the association.
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}
}
}
// release the old value (outside of the lock).
if (old_association.hasValue()) ReleaseValue()(old_association);
}

内存管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static id acquireValue(id value, uintptr_t policy) {
switch (policy & 0xFF) {
case OBJC_ASSOCIATION_SETTER_RETAIN:
return objc_retain(value);
case OBJC_ASSOCIATION_SETTER_COPY:
return ((id(*)(id, SEL))objc_msgSend)(value, SEL_copy);
}
return value;
}

static void releaseValue(id value, uintptr_t policy) {
if (policy & OBJC_ASSOCIATION_SETTER_RETAIN) {
return objc_release(value);
}
}

ObjcAssociation old_association(0, nil);
id new_value = value ? acquireValue(value, policy) : nil;
{
old_association = ...
}
if (old_association.hasValue()) ReleaseValue()(old_association);

我们摘出与对象内存相关的代码仔细分析下,首先把新传入的对象,根据协议进行retain/copy,在赋值的过程中获取旧值,在方法结束前release


赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
//需要赋值
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
//找到了这个对象的关联表
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
//找到了这个key的关联对象
old_association = j->second;
j->second = ObjcAssociation(policy, new_value);
} else {
//没找到,新增一个关联
(*refs)[key] = ObjcAssociation(policy, new_value);
}
} else {
//没找到,创建一个新的关联表
ObjectAssociationMap *refs = new ObjectAssociationMap;
associations[disguised_object] = refs;
(*refs)[key] = ObjcAssociation(policy, new_value);
object->setHasAssociatedObjects();
}
}

先了解一下AssociationsManagerAssociationsHashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
class AssociationsManager {
static AssociationsHashMap *_map;
public:
AssociationsHashMap &associations() {
if (_map == NULL)
_map = new AssociationsHashMap();
return *_map;
}
};

class AssociationsHashMap : public unordered_map<disguised_ptr_t, ObjectAssociationMap *, DisguisedPointerHash, DisguisedPointerEqual, AssociationsHashMapAllocator>;

class ObjectAssociationMap : public std::map<void *, ObjcAssociation, ObjectPointerLess, ObjectAssociationMapAllocator>;

AssociationsManager通过一个以指针地址为主键,值为关联表的哈希表,来管理应用内所有的关联对象。

首先以对象的指针地址去寻找关联表,再通过指定的键值查找关联关系,从而获取关联对象。

删除

1
2
3
4
5
6
7
8
9
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
refs->erase(j);
}
}

和修改方法类似,找到关联关系后,执行哈希表的erase方法删除。


Getter实现

objc_getAssociatedObject实际调用的是_object_get_associative_reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
id _object_get_associative_reference(id object, void *key) {
id value = nil;
uintptr_t policy = OBJC_ASSOCIATION_ASSIGN;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
disguised_ptr_t disguised_object = DISGUISE(object);
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
ObjectAssociationMap *refs = i->second;
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
ObjcAssociation &entry = j->second;
value = entry.value();
policy = entry.policy();
if (policy & OBJC_ASSOCIATION_GETTER_RETAIN) {
objc_retain(value);
}
}
}
}
if (value && (policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE)) {
objc_autorelease(value);
}
return value;
}

查找哈希表的方法和Setter一样,区别在于如果策略中需要retain和autorelease的话,都需要处理。那么是怎么约定这些策略呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum { 
OBJC_ASSOCIATION_SETTER_ASSIGN = 0,
OBJC_ASSOCIATION_SETTER_RETAIN = 1,
OBJC_ASSOCIATION_SETTER_COPY = 3, // NOTE: both bits are set, so we can simply test 1 bit in releaseValue below.
OBJC_ASSOCIATION_GETTER_READ = (0 << 8),
OBJC_ASSOCIATION_GETTER_RETAIN = (1 << 8),
OBJC_ASSOCIATION_GETTER_AUTORELEASE = (2 << 8)
};

typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};

OBJC_ASSOCIATION_RETAIN = 01401,其中01401开头是0,所以是八进制数字,翻译为二进制就是0000 0011 0000 0001,取位判断就是OBJC_ASSOCIATION_SETTER_RETAIN OBJC_ASSOCIATION_GETTER_RETAIN OBJC_ASSOCIATION_GETTER_AUTORELEASE

在保存的时候,需要retain,在获取的时候,需要先retain增加引用计数,再执行autorelease等待释放,从而实现原子性。

Remove实现

objc_removeAssociatedObjects会判断对象是否存在关联,然后再执行_object_set_associative_reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void _object_remove_assocations(id object) {
vector< ObjcAssociation,ObjcAllocator<ObjcAssociation> > elements;
{
AssociationsManager manager;
AssociationsHashMap &associations(manager.associations());
if (associations.size() == 0) return;
disguised_ptr_t disguised_object = DISGUISE(object);
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// copy all of the associations that need to be removed.
ObjectAssociationMap *refs = i->second;
for (ObjectAssociationMap::iterator j = refs->begin(), end = refs->end(); j != end; ++j) {
elements.push_back(j->second);
}
// remove the secondary table.
delete refs;
associations.erase(i);
}
}
// the calls to releaseValue() happen outside of the lock.
for_each(elements.begin(), elements.end(), ReleaseValue());
}

实现方式也可以看出为什么在介绍里不推荐使用,因为会遍历所有的关联对象,并且全部释放,可能会造成别的模块功能缺陷。

判断关联对象

比较有意思的是判断对象是否有关联对象的实现。

1
2
3
4
5
6
inline bool objc_object::hasAssociatedObjects()
{
if (isTaggedPointer()) return true;
if (isa.nonpointer) return isa.has_assoc;
return true;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
inline void objc_object::setHasAssociatedObjects()
{
if (isTaggedPointer()) return;

retry:
isa_t oldisa = LoadExclusive(&isa.bits);
isa_t newisa = oldisa;
if (!newisa.nonpointer || newisa.has_assoc) {
ClearExclusive(&isa.bits);
return;
}
newisa.has_assoc = true;
if (!StoreExclusive(&isa.bits, oldisa.bits, newisa.bits)) goto retry;
}

默认返回的结果都是true,只有在64位系统下,才保存一个标记位。这么处理我推测是为了加快释放周期速度,在析构对象时,会根据这个方法判断是否需要释放关联对象。试想如果每次都查询哈希表,执行效率必定会降低,不如都先通过,之后再做处理。

关于nonpointer不在本文介绍范围内,简单描述为在64位系统下,指针地址保存不仅仅为内存地址,还存有其他标记信息,包括本文涉及的has_assoc。

taggedPointer是一种优化策略,把简单的数字或字符串信息直接保存在指针地址中,从而不申请额外内存加快运行效率。

总结

关联对象的实现不复杂,保存的方式为一个全局的哈希表,存取都通过查询表找到关联来执行。哈希表的特点就是牺牲空间换取时间,所以执行速度也可以保证。


问答

关联对象有什么应用?

关联对象可以在运行时给指定对象绑定一个有生命周期的变量。

1.由于不改变原类的实现,所以可以给原生类或者是打包的库进行扩展,一般配合Category实现完整的功能。

2.ObjC类定义的变量,由于runtime的特性,都会暴露到外部,使用关联对象可以隐藏关键变量,保证安全。

3.可以用于KVO,使用关联对象作为观察者,可以避免观察自身导致循环。

系统如何管理关联对象?

系统通过管理一个全局哈希表,通过对象指针地址和传递的固定参数地址来获取关联对象。根据setter传入的参数协议,来管理对象的生命周期。

其被释放的时候需要手动将其指针置空么?

当对象被释放时,如果设置的协议是OBJC_ASSOCIATION_ASSIGN,那么他的关联对象不会减少引用计数,其他的协议都会减少从而释放关联对象。

unsafe_unretain一般认为外部有对象控制,所以对象不用处理,因此不管什么协议,对象释放时都无需手动讲关联对象置空。