一些语法 更改对象的类/获取对象的类
1 2 3 4 CustomClass *class1 = [[CustomClass alloc]init]; Class aclass =object_set Class(class1, [TestClass class]); NSLog(@"aclass--%@" ,NSStringFromClass(aclass)); NSLog(@"class1--%@" ,NSStringFromClass([class1 class]));
获取对象的类名
1 2 3 4 CustomClass *class = [[CustomClass alloc]init]; const char *name = object_getClassName(class); NSString *className = [NSString stringWithUTF8String:name]; NSLog(@"%@" ,className);
获取一个类的所有方法, 所有的get set方法都能得到 还有公有的和私有的方法也能得到
1 2 3 4 5 6 7 8 9 10 unsigned int count = 0; Method *methods = class_copyMethodList([CustomClass class], &count); for (int i=0; i<count; i++){ SEL name =method_getName(methods[i]); const char *str = sel_getName(name); NSString *str1 = [NSString stringWithUTF8String:str]; NSLog(@"%@" ,str1); } free(methods);
获取一个类的所有属性,私有成员变量也可以得到
1 2 3 4 5 6 7 8 9 unsigned int count = 0; objc_property_t *properties = class_copyPropertyList([CustomClass class], &count); for (int i=0; i<count; i++){ const char *propertyname = property_getName(properties[i]); NSString *str = [NSString stringWithUTF8String:propertyname]; NSLog(@"%@" ,str); } free(properties);
常见的使用 对象归档解档时需要的编码操作,假设对象有多个成员变量
1 2 3 4 @property(nonatomic,assign)int age; @property(nonatomic,copy)NSString * name; @property(nonatomic,assign)float *height; @property(nonatomic,copy)NSString *hobby;
归档处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -(void)encodeWithCoder:(NSCoder *)aCoder { unsigned int count=0; // 获取所有从成员变量 Ivar *ivars = class_copyIvarList([person class], &count); for (int i=0; i<count; i++){ Ivar ivar = ivars[i]; const char *name = ivar_getName(ivar); NSLog(@"%s" ,name); // 归档 NSString *key = [NSString stringWithUTF8String:name]; id value = [self valueForKey:key]; [aCoder encodeObject:value for Key:key]; } free(ivars); }
解档处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 -(instancetype)initWithCoder:(NSCoder *)aDecoder { if (self=[super init]){ unsigned int count =0; Ivar *ivars = class_copyIvarList([person class], &count); for (int i=0; i<count; i++){ Ivar ivar = ivars[i]; const char *name = ivar_getName(ivar); // 解档 NSString *key = [NSString stringWithUTF8String:name]; id value =[aDecoder decodeObjectForKey:key];大专栏 Runtime常见使用 pan>[self set Value:value for Key:key]; } free(ivars); } return self;}
给分类增加成员变量
1 2 3 4 5 6 7 8 9 10 @property(nonatomic,copy)NSString *name; static const char *key = "name" ; -(void)set Name:(NSString *)name { objc_set AssociatedObject(self, key, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } -(NSString *)name { return objc_getAssociatedObject(self, key);}
字典转模型
供解析的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 NSDictionary *dict3 = @{ @"statuses" : @[ @{ @"text" : @"今天天气真不错!" , @"user" : @{ @"name" : @"Rose" , @"icon" : @"nami.png" } }, @{ @"text" : @"明天去旅游了" , @"user" : @{ @"name" : @"Jack" , @"icon" : @"lufy.png" } } ], @"ads" : @[ @{ @"image" : @"ad01.png" , @"url" : @"http://www.ad01.com" }, @{ @"image" : @"ad02.png" , @"url" : @"http://www.ad02.com" } ], @"totalNumber" : @"2014" , @"previousCursor" : @"13476589" , @"nextCursor" : @"13476599" };
为nsobject增加一个分类。传入一个字典参数。
获得全体成员变量 Ivar *ivarList = class_copyIvarList(self, &count);
获得每个成员变量的名称 NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
取出字典中该name名称的对应的value值,id value = Dict[key];判断value是什么类型
如果value是字典类型,需要得到该成员变量的类型NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];并将该类型转为class, Class modelClass = NSClassFromString(type);再来递归调用。
如果value是数组,就需要了解该数组中装的到底是什么类型的变量,在分类中需要定义协议
1 2 3 @protocol ModelDelegate <NSObject> +(NSDictionary*)objectClassInArray; @end
并在成员变量包含数组的类中实现协议方法
1 2 3 4 5 6 7 + (NSDictionary *)objectClassInArray { return @{@"statuses" : @"SugarStatues" , @"ads" : @"SugarADs" , }; }
得到当前key对应的数组中变量的类型(得到的string要转化成class类型),遍历数组中的成员,将字典转为模型。
DEMO Demo链接地址