作为手艺人还是多学点东西好,不然当你四十岁被公司开了,或者行业不景气了想要跳出这个坑,没有点手艺咋混zzz. web几经在学了貌似web也要.......
不跟你多比比 下面这个分类解决的问题就是 不用手敲model.h 的代码其实也可以不用敲model.m的代码(相信没几个人是手敲的).我只是看到有提出这个需求自己 简单写一个.
链接地址git
上代码
NSObject.h的分类
// NSObject+CreatProperty.h
// 一行代码生成Model.h
// Copyright © 2017年 3D. All rights reserved.
#import <Foundation/Foundation.h>
@interface NSObject (CreatProperty)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict andClassStr:(NSString *)classStr andPrdfix:(NSString *)prdfix;
@end
NSObject.m的分类
// NSObject+CreatProperty.m
// 一行代码生成Model.h
//
// Created by 3D on 17/2/27.
// Copyright © 2017年 3D. All rights reserved.
#import "NSObject+CreatProperty.h"
@implementation NSObject (CreatProperty)
+ (void)createPropertyCodeWithDict:(NSDictionary *)dict andClassStr:(NSString *)classStr andPrdfix:(NSString *)prdfix
{
//目前分类中遍历属性类型 还没加完全 而且字典中的NSNumber 都是生的int 先意思一下以后再完善😁
NSMutableString *strM = [NSMutableString string];
// 遍历字典
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull propertyName, id _Nonnull value, BOOL * _Nonnull stop) {
// NSLog(@"%@ %@",propertyName,[value class]);
NSString *code;
if ([value isKindOfClass:[NSString class]]) {
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",propertyName]
;
}else if ([value isKindOfClass:[NSNumber class]]){
//这里写的不完善 暂时都用int 保存
code = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",propertyName]
;
}else if ([value isKindOfClass:[NSArray class]]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",propertyName]
;
[self createPropertyCodeWithKey:propertyName andArray:value andPrdfix:prdfix];
}else if ([value isKindOfClass:[NSDictionary class]]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;",[NSString stringWithFormat:@"%@%@Model",prdfix,propertyName],propertyName]
;
[self createPropertyCodeWithKey:propertyName andDIC:value andPrdfix:prdfix];
}else if ([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",propertyName]
;
}
[strM appendFormat:@"\n%@\n",code];
}];
NSString *top = nil;
top = [NSString stringWithFormat:@"@interface %@ : NSObject",classStr];
NSString *bottom = @"@end";
NSString *allStr= [NSString stringWithFormat:@"\n%@\n%@\n%@\n\n",top,strM,bottom];
const char *cString1 = [allStr cStringUsingEncoding:NSUTF8StringEncoding];
printf("%s",cString1); //这里用c来打印免打印一写不需要的 系统打印
}
+ (void)createPropertyCodeWithKey:(NSString *)key andArray:(NSArray *)arr andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
[self createPropertyCodeWithDict:arr[0] andClassStr:classStr andPrdfix:prdfix];
});
}
+ (void)createPropertyCodeWithKey:(NSString *)key andDIC:(NSDictionary *)dic andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
[self createPropertyCodeWithDict:dic andClassStr:classStr andPrdfix:prdfix];
});
}
@end
关键点就是下面两个方法
-
(void)createPropertyCodeWithKey:(NSString *)key andArray:(NSArray *)arr andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
[self createPropertyCodeWithDict:arr[0] andClassStr:classStr andPrdfix:prdfix];
});
} -
(void)createPropertyCodeWithKey:(NSString *)key andDIC:(NSDictionary *)dic andPrdfix:(NSString *)prdfix{
dispatch_async(dispatch_get_main_queue(), ^{
NSString *classStr = [NSString stringWithFormat:@"%@%@Model",prdfix,key];
[self createPropertyCodeWithDict:dic andClassStr:classStr andPrdfix:prdfix];
});
}
这连个方法是异步主队列 上次我记得已经我已经解释过了 GCD的一些知识
我们要打印model的属性总是希望一层一层的打印.但是在递归打印的时候有可能是这个样的model模型例如
@{@"a":@"属性1"
@"b":@"属性2"
@"c":@"属性3:
@"d":@{@"aa":@"我是对象"}
@"e":@"属性4"}
我们想生成
xxxxxxxxxxx a
xxxxxxxxxxx b
xxxxxxxxxxx c
xxxxxxxxxxx d
xxxxxxxxxxx e
然后在生成
xxxxxxxxxxx aa
你看上面其实是递归操作 遇见value是字典 或者 数组 就有调用
自己那么我们 这个时候第一层还没打印玩 但是又要调用自己打印
下一层我们就可以 把打印下一层的 任务放在异步主队列 (当然打
印调用这个方法前提是主队列) 异步主队列 第一不会影响 当前线
程的任务(打印下面的属性) 当下面的属性打印完了(主队列空闲了)
就会执行我们第一次方法主队列的任务,依次进行下去.最后我们只
要打印台粘贴复制
下面是这个分类的用法
// ViewController.m
// 一行代码生成Model.h
// Created by 3D on 17/2/27.
// Copyright © 2017年 3D. All rights reserved.
#import "ViewController.h"
#import "LCTestModel.h"
#import "NSObject+CreatProperty.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
//使用前请把xcode8自动打印的log关掉比较 打印台比较清爽
[super viewDidLoad];
NSDictionary *dic0 = @{@"thumbnail_pic":@"http://ww4.sinaimg.cn/thumbnail/80292f4btw1eqi01myf23j20br08ogm0.jpg",
@"name":@"对象中有个数组数组里面是对象"};
NSDictionary *dic = @{@"attitudes_count":@3,
@"created_at":@"刚刚",
@"idstr":@"3824316141411024",
@"pic_urls":@[dic0],
@"user":@{@"name":@"对象面有对象",
@"vip":@1}
};
NSDictionary *dic1 = @{@"attitudes_count":@3,
@"created_at":@"刚刚",
@"idstr":@"3824316141411024",
@"pic_urls":@[dic0],
@"user":@{@"name":@"猪猪爱讲冷笑话",
@"vip":@1}
};
NSDictionary *dic2 = @{@"attitudes_count":@3,
@"created_at":@"刚刚",
@"idstr":@"3824316141411024",
@"pic_urls":@[dic0],
@"user":@{@"name":@"猪猪爱讲冷笑话",
@"vip":@1}
};
NSDictionary *dic3 = @{@"statuses":@[dic,dic1,dic2],
@"total_number":@1091};
[LCTestModel createPropertyCodeWithDict:dic3 andClassStr:NSStringFromClass([LCTestModel class]) andPrdfix:@"LC"];//@"LC"就是 model前缀自己定义我叫李长城我就定义LC
}
@end
打印台的效果图就是
WechatIMG52.jpeg
网友评论