类扩展和分类的区别
-
category:就是类别,也叫分类,专门用来给类添加新的方法,不能给类添加成员属性,但可以使用
runtime关联对象,使用property定义变量,只会生成变量的gettter,setter方法的声明,不能生成方法实现和带下划线的成员变量。 -
类扩展:可以给类添加成员属性,但是是私有变量。可以给类添加方法,也只是私有方法。
关联对象分析
在iOS分类中,我们可以使用在分类中添加关联属性,主要是通过void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)和id objc_getAssociatedObject(id object, const void *key)这两个函数进行设置,通过objc源码观察一下具体实现过程
objc_setAssociatedObject
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;
}
首先会根据内存管理策略,来进行内存管理,分别进行retain和copy操作。
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;
//初始化 HashMap
AssociationsHashMap &associations(manager.associations());
//当前对象的地址按位取反(key)
disguised_ptr_t disguised_object = DISGUISE(object);
if (new_value) {
// break any existing association.
//<fisrt : disguised_object , second : ObjectAssociationMap>
AssociationsHashMap::iterator i = associations.find(disguised_object);
if (i != associations.end()) {
// secondary table exists
ObjectAssociationMap *refs = i->second;
//<fisrt : 标识(自定义的) , second : ObjcAssociation>
ObjectAssociationMap::iterator j = refs->find(key);
if (j != refs->end()) {
old_association = j->second;
//ObjcAssociation
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);
}
然后,创建 ObjcAssociation对象,存储在 AssociationsHashMap中
objc_getAssociatedObject
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;
}
在读取的过程中,根据对象地址按位取反读取AssociationsHashMap对象,然后找到对应的ObjectAssociationMap对象,然后取出value值
关联属性的释放
- (void)dealloc {
_objc_rootDealloc(self);
}
如果对象有关联属性,在对象dealloc时,会调用 object_dispose((id)this),然后调用 objc_destructInstance(obj),如果该对象有关联属性,会调用 _object_remove_assocations
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());
}
从而完成了关联属性的释放。











网友评论