前几天有人问我一个问题:为什么分类不能自动创建get set方法。老实说,笔者从来没有去思考过这个问题。于是这次通过代码实践跟 runtime 源码来探究这个问题。
为了能减少输出类数据的代码工作,笔者基于 NSObject 的分类封装了一套代码
其中输出类实例变量的具体代码:
- (void)logIvarsWithExpReg: (NSString *)expReg customed: (BOOL)customed {
[NSObject kRecordOBJ];
unsigned int ivarCount;
Ivar * ivars = class_copyIvarList([self class], &ivarCount);
for (int idx = 0; idx < ivarCount; idx++) {
Ivar ivar = ivars[idx];
NSString * ivarName = [NSString stringWithUTF8String: ivar_getName(ivar)];
if (customed && [kOBJIvarNames containsObject: ivarName]) {
continue;
}
if (expReg && !kValidExpReg(ivarName, expReg)) {
continue;
}
printf("ivar: %s --- %s/n", NSStringFromClass([self class]).UTF8String, ivarName.UTF8String);
}
free(ivars);
}
+(void)kRecordOBJ 采用 dispatch_once 的方式将 NSObject 存在的数据存储到三个数组中,用来排除父类的数据输出
正常创建类
@interface Person: NSObject {
int _pId;
}
@property (nonatomic, copy) NSString * name;
@property (nonatomic, assign) NSUInteger age;
@end
int main(int argc, char * argv[]) {
@autoreleasepool {
Person * p = [[Person alloc] init];
[p logCustomIvars];
[p logCustomMethods];
[p logCustomProperties];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
} 运行结果:属性 name 和 age 生成了对应的 _propertyName 的实例变量以及 setter 和 getter
动态生成属性 age
@implementation Person @dynamic age; @end
运行结果:缺少了 _age 变量以及对应的 setAge: 和 age 方法
手动实现 setter/getter
@implemetation Person
@dynamic age;
- (void)setAge: (NSUInteger)age {}
- (NSUInteger)age { return 18; }
@end 输出结果:未生成 _age 实例变量
手动实现 _pId 的 setter/getter
@implemetation Person
@dynamic age;
- (void)setAge: (NSUInteger)age {}
- (NSUInteger)age { return 18; }
- (void)setPId: (int)pId { _pId = pId; }
- (int)pId { return _pId; }
@end
[p setValueForKey: @"pId"]; 运行结果: KVC 的访问会触发 setter 方法, _pId 除了无法通过点语法访问外,其他表现与 @property 无异
通过上面的几段试验,可以得出 @property 的公式:
分类中添加 weigh 和 height 属性
@interface Person (category) @property (nonatomic, assign) CGFloat weigh; @property (nonatomic, assign) CGFloat height; @end
运行结果: weigh 和 height 未生成实例变量以及对应的 setter/getter ,与 @dynamic 修饰的 age 表现一致
使用 @synthesize 自动合成 setter/getter 方法时编译报错
手动实现 setter/getter
@implemetation Person (category)
- (void)setWeigh: (CGFloat)weigh {}
- (CGFloat)weigh { return 150; }
@end 运行结果:与 @dynamic age 后重写其 setter/getter 表现一致
动态绑定属性来实现 setter/getter
void * kHeightKey = &kHeightKey;
@implemetation Person (category)
- (void)setWeigh: (CGFloat)weigh {}
- (CGFloat)weigh { return 150; }
- (void)setHeight: (CGFloat)height {
objc_setAssociatedObject(self, kHeightKey, @(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGFloat)height {
return return [objc_getAssociatedObject(self, kHeightKey) doubleValue];;
}
@end
[p logCustomIvars]
[p logCustomMethods];
[p logCustomProperties];
CGFloat height = 180;
p.height = 180;
height = p.height;
[p logCustomIvars]
[p logCustomMethods];
[p logCustomProperties]; 运行结果:动态绑定前后 ivar 没有发生任何变化
通过代码实验,可以得出下面两个结论:
@dynamic property ivar 的情况下无法使用 @synthesize 自动合成属性 以及一个猜想:
ivar 通过runtime动态创建类验证猜想:
int main(int argc, char * argv[]) {
NSString * className = @"Custom";
Class customClass = objc_allocateClassPair([NSObject class], className.UTF8String, 0);
class_addIvar(customClass, @"ivar1".UTF8String, sizeof(NSString *), 0, "@");
objc_property_attribute_t type1 = { "T", "@/"NSString/"" };
objc_property_attribute_t ownership1 = { "C", "N" };
objc_property_attribute_t atts1[] = { type1, ownership1 };
class_addProperty(customClass, "property1", atts1, 2);
objc_registerClassPair(customClass);
id instance = [[customClass alloc] init];
NSLog(@"/nLog Ivars ===================");
[instance logCustomIvars];
NSLog(@"/nLog methods ===================");
[instance logCustomMethods];
NSLog(@"/nLog properties ===================");
[instance logCustomProperties];
class_addIvar(customClass, @"ivar2".UTF8String, sizeof(NSString *), 0, "@");
objc_property_attribute_t type2 = { "T", "@/"NSString/"" };
objc_property_attribute_t ownership2 = { "C", "N" };
objc_property_attribute_t atts2[] = { type2, ownership2 };
class_addProperty(customClass, "property2", atts2, 2);
instance = [[customClass alloc] init];
NSLog(@"/nLog Ivars ===================");
[instance logCustomIvars];
NSLog(@"/nLog methods ===================");
[instance logCustomMethods];
NSLog(@"/nLog properties ===================");
[instance logCustomProperties];
}
运行结果:在调用 class_registerClassPair 后,添加 ivar 失败
objc_class 的结构体定义如下:
struct objc_class : objc_object {
Class superclass;
const char *name;
uint32_t version;
uint32_t info;
uint32_t instance_size;
struct old_ivar_list *ivars;
struct old_method_list **methodLists;
Cache cache;
struct old_protocol_list *protocols;
// CLS_EXT only
const uint8_t *ivar_layout;
struct old_class_ext *ext;
}
ps: 在新版本中结构体内部已经发生了大改,但是内部的属性大致上仍是这些
这里面有个重要的属性 ivar_layout ,顾名思义存放的是变量的位置属性,与之对应的还有一个 weakIvarLayout 变量,不过在默认结构中没有出现。这两个属性用来记录 ivar 哪些是 strong 或者 weak ,而这个记录操作在 runtime 阶段已经被确定好。正由于如此,这极有可能是 ivar 无法在类被加载后继续添加的原因之一。 ivar_layout 的更多了解可以参照 Objective-C Class Ivar layout 一文
import 操作帮助编译检查和链接过程,但是在 category 的加载过程中,不会将扩展的内容添加到原始的类结构中。 runtime 对于 category 的加载过程可以简单的分成下面几步(摘自 objc category的密码 ):
objc runtime 的加载入口是一个叫 _objc_init 的方法,在 library 加载前由 libSystem dyld 调用,进行初始化操作 map_images 方法将文件中的 image map 到内存 _read_images 方法初始化 map 后的 image ,这里面干了很多的事情,像 load 所有的类、协议和 category ,著名的 + load 方法就是这一步调用的 -仔细看 category 的初始化,循环调用了 _getObjc2CategoryList 方法,这个方法拿出来看看: 这一切的过程发生在 _objc_init 函数中,函数实现如下
简单来说在 load_images 函数中最终会走到下面的代码调用来加载所有的类以及类的分类
根据上面的代码加上 runtime 的加载顺序,可以继续推出:
@dynamic 实际上是将属性的加载推迟到类加载完成后 另外,前面也说过在缺少 ivar 的情况下无法自动合成 setter/getter ,除了 category 本身是不被添加到类结构中的,所以无法使用类结构的 ivar 合成属性外,还有分类自身结构的问题
struct category_t {
const char *name; /// 类名
classref_t cls; /// 类指针
struct method_list_t *instanceMethods; /// 实例方法
struct method_list_t *classMethods; /// 类方法
struct protocol_list_t *protocols; /// 扩展的协议
struct property_list_t *instanceProperties; /// 扩展属性
method_list_t *methodsForMeta(bool isMeta) { ... }
property_list_t *propertiesForMeta(bool isMeta) { ... }
};
可以看到分类结构本身是不存在 ivar 的容器的,因此缺少了自动合成属性的条件。最后还有一个问题,我们在使用 objc_associate 系列函数绑定属性的时候这些变量存储在了哪里?
在 runtime 底层,存在一个类型为 AssociationHashMap 的哈希映射表保存着对象动态添加的属性,每个对象以自身地址为 key 维护着一个绑定属性表,我们动态添加的属性就都存储在这个表里。
转载请注明原文地址及作者