关联对象
关联对象一般用来配合 Category 使用,在 Category 中声明属性时编译器只会自动生成 Getter、Setter 的接口声明,而不会自动生成成员变量和 Getter、Setter 的方法实现。所以说 Category 中不能直接添加成员变量,但是可以通过关联对象来间接实现。
相关的 API
// 设置关联对象的 API
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
id _Nullable value, objc_AssociationPolicy policy);
// 获取关联对象的 API
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key);
// 移除所有关联对象的 API
objc_removeAssociatedObjects(id _Nonnull object);
// 关联策略
typedef OBJC_ENUM(uintprt_t, objc_AssociationPolicy) {
OBJC_ASSOCIATION_ASSIGN = 0, // assign
OBJC_ASSOCIATION_COPY_NONATOMIC = 1, // copy, nonatomic
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 3, // retain, nonatomic
OBJC_ASSOCIATION_RETAIN = 01401, // retain, atomic
OBJC_ASSOCIATION_COPY = 01403 // copy, atomic
}
关联对象的使用
@interface Demo (Test)
@property (nonatomic, assign) NSInteger ivar;
@end
@implementation Demo
- (void)setIvar:(NSInteger)ivar {
objc_setAssociatedObject(self, @selector(iavr),
@(ivar), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSInteger)ivar {
// _cmd = @selector(ivar)
return objc_getAssociatedObject(self, _cmd);
}
@end
关联对象的底层实现
关联对象存储在一个全局的 AssociationsManager 中。
AssociationsManager:
// class AssociationsManager manages a lock / hash table singleton pair.
// Allocating an instance acquires the lock, and calling its assocations()
// method lazily allocates the hash table.
spinlock_t AssociationsManagerLock;
class AssociationsManager {
// associative references: object pointer -> PtrPtrHashMap.
static AssociationsHashMap *_map;
public:
AssociationsManager() { AssociationsManagerLock.lock(); }
~AssociationsManager() { AssociationsManagerLock.unlock(); }
AssociationsHashMap &associations() {
if (_map == NULL)
_map = new AssociationsHashMap();
return *_map;
}
};
<br />
AssociationsHashMap:
AssociationsHashMap *AssociationsManager::_map = NULL;
<br />
ObjectAssociationMap:
class ObjectAssociationMap : public std::map<void *, ObjcAssociation, ObjectPointerLess, ObjectAssociationMapAllocator> {
public:
void *operator new(size_t n) { return ::malloc(n); }
void operator delete(void *ptr) { ::free(ptr); }
};
<br />
ObjcAssociation:
class ObjcAssociation {
uintptr_t _policy;
id _value;
public:
ObjcAssociation(uintptr_t policy, id value) : _policy(policy), _value(value) {}
ObjcAssociation() : _policy(0), _value(nil) {}
uintptr_t policy() const { return _policy; }
id value() const { return _value; }
bool hasValue() { return _value != nil; }
};
网友评论