iOS自动生成模型属性代码

2017-11-23  本文已影响25人  笨小孩_松

每次声明一个model要根据后台返回写好多属性感觉很蛋疼, 写了一个NSObject的分类分享出来, 只需要传入字典把控制台打印的粘贴到model的.h文件(类型判断不是很完整需要自己根据需要加一下)

#import"NSObject+model.h"

@implementationNSObject (model)

+ (void)createPropertyCodeWithDic:(NSDictionary*)dic{

NSMutableString*str = [NSMutableStringstring];

[dicenumerateKeysAndObjectsUsingBlock:^(id_Nonnullkey,id_Nonnullobj,BOOL*_Nonnullstop) {

//NSLog(@"%@ %@", key, [obj class]);

NSString*code;

if([objisKindOfClass:NSClassFromString(@"__NSCFNumber")]) {

code = [NSStringstringWithFormat:@"@property (nonatomic, assign) int %@;", key];

}elseif([objisKindOfClass:NSClassFromString(@"__NSCFString")]){

code = [NSStringstringWithFormat:@"@property (nonatomic, strong) NSString *%@;", key];

}

elseif([objisKindOfClass:NSClassFromString(@"__NSCFDictionary")]){

code = [NSStringstringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;", key];

}

elseif([objisKindOfClass:NSClassFromString(@"__NSCFBoolean")]){

code = [NSStringstringWithFormat:@"@property (nonatomic, assign) BOOL %@;", key];

}

if(code) {

[strappendFormat:@"\n%@\n", code];

}

}];

NSLog(@"%@", str);

}

@end

这里附上链接:点这里

上一篇下一篇

猜你喜欢

热点阅读