runtime是oc的真面目。oc底层的一套c语言API.
unsigned int count; //获取属性列表 objc_property_t *propertyList = class_copyPropertyList([self class], &count); for (unsigned int i=0; i<count; i++) { const char *propertyName = property_getName(propertyList[i]); NSLog(@"property---->%@", [NSString stringWithUTF8String:propertyName]); } //获取方法列表 Method *methodList = class_copyMethodList([self class], &count); for (unsigned int i; i<count; i++) { Method method = methodList[i]; NSLog(@"method---->%@", NSStringFromSelector(method_getName(method))); } //获取成员变量列表 Ivar *ivarList = class_copyIvarList([self class], &count); for (unsigned int i; i<count; i++) { Ivar myIvar = ivarList[i]; const char *ivarName = ivar_getName(myIvar); NSLog(@"Ivar---->%@", [NSString stringWithUTF8String:ivarName]); } //获取协议列表 __unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count); for (unsigned int i; i<count; i++) { Protocol *myProtocal = protocolList[i]; const char *protocolName = protocol_getName(myProtocal); NSLog(@"protocol---->%@", [NSString stringWithUTF8String:protocolName]); }
应用1:KVC字典转模型
Class clazz = Person.class; unsigned int count = 0; Person *person = [[Person alloc]init]; NSDictionary *dict = @{@"name":@"zhangsan",@"age":@19, @"height": @1.75}; Ivar *ivars = class_copyIvarList(clazz, &count); for (int i = 0; i < count; i++) { const char *cname = ivar_getName(ivars[i]); NSString *name = [NSString stringWithUTF8String:cname]; NSString *key = [name substringFromIndex:1]; const char *coding = ivar_getTypeEncoding(ivars[i]); // 获取类型 NSString *strCode = [NSString stringWithUTF8String:coding]; id value = dict[key]; if ([strCode isEqualToString:@"f"]) {// 判断类型是否是float value = @(0.0); } [person setValue:value forKey:key]; } NSLog(@"%@", person);
应用2:NSCoding归档和解档
- (void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count = 0; Ivar *ivars = class_copyIvarList(self.class, &count); for (int i = 0; i < count; i++) { const char *cname = ivar_getName(ivars[i]); NSString *name = [NSString stringWithUTF8String:cname]; NSString *key = [name substringFromIndex:1]; id value = [self valueForKey:key]; // 取出key对应的value [aCoder encodeObject:value forKey:key]; // 编码 } } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { unsigned int count = 0; Ivar *ivars = class_copyIvarList(self.class, &count); for (int i = 0; i < count; i++) { const char *cname = ivar_getName(ivars[i]); NSString *name = [NSString stringWithUTF8String:cname]; NSString *key = [name substringFromIndex:1]; id value = [aDecoder decodeObjectForKey:key]; // 解码 [self setValue:value forKey:key]; // 设置key对应的value } } return self; }
其他应用场景: