前言:
如果你的项目有一个接口返回的字段超级多,而且你需要创建一个Model文件来处理业务,那么请开心的阅读本文,我写了一个工具类(其实就一个方法),傻瓜式操作,一行代码自动生成 你要敲好半天的属性定义代码
例如:
//为了创造字段多的视觉体验,我复制了一大堆,如果你们看不下去,我接受挨打....
@property (nonatomic ,copy) NSString * userName1;
@property (nonatomic ,strong) NSArray * userArr;
@property (nonatomic ,copy) NSString * userName3;
@property (nonatomic , strong) NSDictionary * userInfo;
@property (nonatomic ,copy) NSString * userName5;
@property (nonatomic ,copy) NSString * userName6;
@property (nonatomic ,copy) NSString * userName7;
@property (nonatomic ,copy) NSString * userName1;
@property (nonatomic , strong) NSArray * userArr;
@property (nonatomic ,copy) NSString * userName3;
@property (nonatomic , strong) NSDictionary * userInfo;
@property (nonatomic ,copy) NSString * userName5;
@property (nonatomic ,copy) NSString * userName6;
@property (nonatomic ,copy) NSString * userName7;
@property (nonatomic ,copy) NSString * userName1;
@property (nonatomic , strong) NSArray * userArr;
@property (nonatomic ,copy) NSString * userName3;
@property (nonatomic , strong) NSDictionary * userInfo;
@property (nonatomic ,copy) NSString * userName5;
@property (nonatomic ,copy) NSString * userName6;
@property (nonatomic ,copy) NSString * userName7;
@property (nonatomic ,copy) NSString * userName1;
@property (nonatomic , strong) NSArray * userArr;
@property (nonatomic ,copy) NSString * userName3;
@property (nonatomic , strong) NSDictionary * userInfo;
@property (nonatomic ,copy) NSString * userName5;
@property (nonatomic ,copy) NSString * userName6;
@property (nonatomic ,copy) NSString * userName7;
言归正传:先上Demo:https://github.com/MrPlusZhao/PropertyNameCreateTool
(我写了随便写的本地的Plist文件,模拟请求的数据返回,大神勿喷..)
使用方法很简单,把你请求回来的dict,传入即可
========就这一行代码========
[TP_PropertyCreateTool PropertyNameCreateFromDict:dict];
========就这一行代码========
方法实现在这里(根据业务需求,提前把业务可能包含的类型准备好就行了),本文只是提供一个思路,在实际应用中,方法里面可以根据自己实际的业务需求,处理一些细节更改.
+ (void)PropertyNameCreateFromDict:(NSDictionary*)dict
{
NSMutableString *mutStr = [NSMutableString string];
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
NSString *resultStr = @"";
if ([value isKindOfClass:NSClassFromString(@"__NSCFString")]) {
resultStr = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",propertyName];
}
else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")])
{
resultStr = [NSString stringWithFormat:@"@property (nonatomic, strong) NSNumber *%@;",propertyName];
}
else if ([value isKindOfClass:NSClassFromString(@"__NSCFArray")])
{
resultStr = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName];
}
else if ([value isKindOfClass:NSClassFromString(@"__NSCFDictionary")])
{
resultStr = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",propertyName];
}
else if ([value isKindOfClass:NSClassFromString(@"__NSCFNumber")])
{
resultStr = [NSString stringWithFormat:@"@property (nonatomic, assign) Int *%@;",propertyName];
}
else if ([value isKindOfClass:NSClassFromString(@"__NSCFDate")])
{
resultStr = [NSString stringWithFormat:@"@property (nonatomic, assign) NSDate *%@;",propertyName];
}
//1:如果识别成功,加入打印的行列
if (resultStr.length > 0) {
[mutStr appendFormat:@"\n%@\n",resultStr];
}
//2:如果识别失败,记录下是哪个属性,根据自己的业务需求,自己添加
else
{
NSLog(@"class == %@, name == %@",[value class],propertyName);
}
}];
NSLog(@"\n====最后得到的打印可以直接复制放到Model.h文件里================\n%@\n====最后得到的打印可以直接复制放到Model.h文件里================",mutStr);
}
感兴趣的小伙伴可以下载试一下.
PS:另外还有一个工具,可以直接生成Model.h和Model.m 文件,有需求的请评论留言.