前言
在上一篇文章中我们探索到类的属性、方法、协议存储的位置,今天来探索一些类的成员变量
1.探究类的成员变量
在2020 WWDC讲解的时候,有下面一张类的结构图
从图中我们很明显可以看到类的成员变量存储在class_ro_t结构体中。那么下面我们来获取一下class_ro_t结构
1.1 获取类的class_ro_t结构
1.2 为什么在class_ro_t与class_rw_ext_t都有方法、属性、协议呢?
struct class_rw_ext_t {
DECLARE_AUTHED_PTR_TEMPLATE(class_ro_t)
class_ro_t_authed_ptr ro;
method_array_t methods;
property_array_t properties;
protocol_array_t protocols;
char *demangledName;
uint32_t version;
};
1.2.1 clean memory与dirty memory
回答这个问题首先先了解一些什么是dirty memory与clean memory
-
clean memory
:是指加载后不会发生更改的内存 -
dirty memory
:是指在进程进行时会发生更改的内存
类结构一经使用就会变成dirty memory,因为运行时会向它写入新的数据. 比如我们可以通过苹果提供的api动态添加一个方法或者一个属性。
dirty memory比clean memory要比消耗的多,因为只要进程在运行,它就必须一直存在。
clean memory可以进程移除,从而节省更多的空间。因为如果你需要clean memory,系统可以从磁盘重新加载。
所以苹果为了节省内存就把class_rw_t分离为class_ro_t只读(clean memory)与class_rw_ext_t可读可写(dirty memory)两部分
class_rw_ext里面的方法、属性、协议是苹果在运行时的时候,从class_ro_t从拷贝的一份。底层源码的实现如下:
class_rw_ext_t *class_rw_t::extAlloc(const class_ro_t *ro, bool deepCopy)
{
runtimeLock.assertLocked();
auto rwe = objc::zalloc();
rwe->version = (ro->flags & RO_META) ? 7 : 0;
method_list_t *list = ro->baseMethods();
if (list) {
if (deepCopy) list = list->duplicate();
rwe->methods.attachLists(&list, 1);
}
// See comments in objc_duplicateClass
// property lists and protocol lists historically
// have not been deep-copied
// This is probably wrong and ought to be fixed some day
property_list_t *proplist = ro->baseProperties;
if (proplist) {
rwe->properties.attachLists(&proplist, 1);
}
protocol_list_t *protolist = ro->baseProtocols;
if (protolist) {
rwe->protocols.attachLists(&protolist, 1);
}
set_ro_or_rwe(rwe, ro);
return rwe;
}
1.3 获取类的成员变量
成员变量在底层是ivar_t结构体
struct ivar_t {
#if __x86_64__
// *offset was originally 64-bit on some x86_64 platforms.
// We read and write only 32 bits of it.
// Some metadata provides all 64 bits. This is harmless for unsigned
// little-endian values.
// Some code uses all 64 bits. class_addIvar() over-allocates the
// offset for their benefit.
#endif
int32_t *offset;
const char *name;
const char *type;
// alignment is sometimes -1; use alignment() instead
uint32_t alignment_raw;
uint32_t size;
uint32_t alignment() const {
if (alignment_raw == ~(uint32_t)0) return 1U << WORD_SHIFT;
return 1 << alignment_raw;
}
};
1.3 类的属性与成员变量的区别
类的属性 = 带下划线的成员变量 + setter + getter方法
属性的setter方法与getter方法
static NSString * _I_ZFPerson_nickName(ZFPerson * self, SEL _cmd) { return (*(NSString **)((char *)self + OBJC_IVAR_$_ZFPerson$_nickName)); }
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool);
static void _I_ZFPerson_setNickName_(ZFPerson * self, SEL _cmd, NSString *nickName) { objc_setProperty (self, _cmd, __OFFSETOFIVAR__(struct ZFPerson, _nickName), (id)nickName, 0, 1); }
static NSInteger _I_ZFPerson_age2(ZFPerson * self, SEL _cmd) { return (*(NSInteger *)((char *)self + OBJC_IVAR_$_ZFPerson$_age2)); }
static void _I_ZFPerson_setAge2_(ZFPerson * self, SEL _cmd, NSInteger age2) { (*(NSInteger *)((char *)self + OBJC_IVAR_$_ZFPerson$_age2)) = age2; }
static NSObject * _I_ZFPerson_obj(ZFPerson * self, SEL _cmd) { return (*(NSObject **)((char *)self + OBJC_IVAR_$_ZFPerson$_obj)); }
static void _I_ZFPerson_setObj_(ZFPerson * self, SEL _cmd, NSObject *obj) { (*(NSObject **)((char *)self + OBJC_IVAR_$_ZFPerson$_obj)) = obj; }
从上面编译的c++代码中我们可以看出,copy修饰
的nickName的setter方法是调用了一个
objc_setProperty方法,strong修饰obj与assign修饰
age2的setter方法是通过获取到成员变量直接赋新值的过程
1.4 探究copy修饰的属性setter方法(objc_setProperty)
void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
{
bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
bool mutableCopy = (shouldCopy == MUTABLE_COPY);
reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
}
static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
{
if (offset == 0) {
object_setClass(self, newValue);
return;
}
id oldValue;
id *slot = (id*) ((char*)self + offset);
if (copy) {
newValue = [newValue copyWithZone:nil];
} else if (mutableCopy) {
newValue = [newValue mutableCopyWithZone:nil];
} else {
if (*slot == newValue) return;
newValue = objc_retain(newValue);
}
if (!atomic) {
oldValue = *slot;
*slot = newValue;
} else {
spinlock_t& slotlock = PropertyLocks[slot];
slotlock.lock();
oldValue = *slot;
*slot = newValue;
slotlock.unlock();
}
objc_release(oldValue);
}
从源码中可以看出set_objc_setProperty就是一个拷贝变量并且赋值的过程。