一、Class
通过之前文章我们可以知道,所有对象都有一个isa指针指向它对应类的Class,而Class则是一个objc_class结构体,结构体中:
- 实例变量列表objc_ivar_list
- 方法列表objc_method_list
Ivar *class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount)
Method *class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
首先我们创建一个Test:
添加一些属性,成员变量,方法等..
@interface Test : NSObject
{
NSString *_address;
NSString *_code;
}
@property(nonatomic, copy) NSString *name;
@property(nonatomic, assign) NSInteger age;
- (void)test1;
- (void)test2;
二、Runtime访问
实例化Test,设置属性与成员变量的值:
Test *test;
test = [[Test alloc] init];
test.name = @"jack";
test.age = 20;
[test setValue:@"one road" forKey:@"_address"];
[test setValue:@"123456" forKey:@"_code"];
获取所有属性:
/// 获取所有属性
- (NSDictionary *)getProperties {
unsigned int count = 0;
// 获取所有类的property,如果没有属性 count 就为0
objc_property_t *properties = class_copyPropertyList([Test class], &count);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i=0; i<count; i++) {
// 获取属性的name和value
const char *propertyName = property_getName(properties[I]);
NSString *name = [NSString stringWithUTF8String:propertyName];
id propertyValue = [test valueForKey:name];
if (propertyValue) {
dict[name] = propertyValue;
}else {
dict[name] = @"value is nil";
}
}
// properties是一个数组指针,需要用free函数来释放内存
free(properties);
return dict;
}
获取所有成员变量:
/// 获取所有成员变量
- (NSDictionary *)getIvars {
unsigned int count = 0;
// 获取类所有的ivar
Ivar *ivars = class_copyIvarList([Test class], &count);
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i=0; i<count; i++) {
// 获取变量的name和value
const char *varName = ivar_getName(ivars[I]);
NSString *name = [NSString stringWithUTF8String:varName];
id varValue = [test valueForKey:name];
if (varValue) {
dict[name] = varValue;
}else {
dict[name] = @"value is nil";
}
}
free(ivars);
return dict;
}
获取所有方法:
/// 获取所有方法
- (NSDictionary *)getMethod {
unsigned int count = 0;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// 获取类所有的method
Method *methods = class_copyMethodList([Test class], &count);
for (int i=0; i<count; i++) {
// 获取方法name
SEL methodSEL = method_getName(methods[i]);
const char *methodName = sel_getName(methodSEL);
NSString *name = [NSString stringWithUTF8String:methodName];
// 获取方法的参数列表
int arguments = method_getNumberOfArguments(methods[I]);
// 因为消息发送的时候会有两个默认的参数(消息接受者和方法名),所以需要减去2
dict[name] = @(arguments-2);
}
free(methods);
return dict;
}
打印结果看看,
NSDictionary *proDic = [self getProperties];
for (NSString *proName in proDic.allKeys) {
NSLog(@"proName:%@, proValue:%@",proName,proDic[proName]);
}
NSDictionary *ivarsDic = [self getIvars];
for (NSString *ivarName in ivarsDic.allKeys) {
NSLog(@"ivarName:%@, ivarValue:%@",ivarName,ivarsDic[ivarName]);
}
for (NSString *methodName in methodDic.allKeys) {
NSLog(@"methodName:%@, argumentsCount:%@",methodName,methodDic[methodName]);
}
从上面输出日志我们可以看到,成员变量的数量多两个,其实每个属性会自动生成一个_XXX的成员变量,并自动生成getter和setter方法,从上面打印内容你可以清晰看出来。
方法列表里多了个.cxx_destruct方法,其实.cxx_destruct方法是为了C++方法析构的,在这里ARC借用了这个方法插入代码实现了自动释放内存的工作