iOS-4 自动生成model属性
2019-08-11 本文已影响0人
leesen
当后台给的参数过多时。可以使用这个分类
传入一个字典,给你一个model属性的代码(可根据自己的参数类型做更改)。
.h文件
@interface NSObject (Property )
- (void)createPropertyCodeWithDict:(NSDictionary *)dict;
@end
.m文件
@implementation NSObject (Property)
-
(void)createPropertyCodeWithDict:(NSDictionary *)dict
{NSMutableString *strM = [NSMutableString string];
// 遍历字典
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
// NSLog(@"%@ %@",propertyName,[value class]);
NSString *code;NSLog(@"%@",[value class]); if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) { code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName] ; }else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")]){ code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",propertyName] ; }else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")]){ code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName] ; }else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")]){ code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSDictionary *%@;",propertyName] ; }else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){ code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName] ; } else if ([value isKindOfClass:NSClassFromString(@"__NSArray0")]){ code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName] ; } else if ([value isKindOfClass:NSClassFromString(@"__NSArrayI")]){ code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",propertyName] ; } else if ([value isKindOfClass:NSClassFromString(@"NSTaggedPointerString")]){ code = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName] ; } [strM appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",strM);
}
@end
用法,直接传入一个字典,会在控制台打印model属性,直接复制粘贴即可
https://www.jianshu.com/p/158d8e28e5cd
参考:
https://www.jianshu.com/p/f30cf05054c6
iOS 效率工具:自动生成 Model 文件