iOS百行代码打造JOSN模型代码生成器

Mac应用,采用xib方式,拖入2个NSTextView

代码比较简单,整个.m文件才百余行代码

ViewController.h

1
2
3
4
5
6
7
8
9
#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController

@property (unsafe_unretained) IBOutlet NSTextView *inputTextView;
@property (unsafe_unretained) IBOutlet NSTextView *outPutTextView;
- (IBAction)autoCodeCreate:(id)sender;

@end

ViewController.m

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "ViewController.h"
#import "JSONUtils.h"

@interface ViewController()
@property(nonatomic,strong) NSMutableArray *arrayModel;
@property(nonatomic,strong) NSDictionary *jsonDict;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}


- (IBAction)autoCodeCreate:(id)sender {
NSString *jsonStr = self.inputTextView.textStorage.string;
//json字符串转json字典,可用
NSDictionary *dic = [self objectFromJSONString:jsonStr];
if(dic==nil){
self.inputTextView.string = @"JSON格式错误";
return;
}
self.jsonDict=dic;
NSString *fileStr=[self autoCodeWithJsonDict:dic modelKey:nil];
NSLog(@"%@", fileStr);
[self.outPutTextView setString:fileStr];

[self handleArrayModel];
}

-(void)handleArrayModel
{
for (NSString *key in self.arrayModel) {
NSDictionary *jsonDict=[self.jsonDict[key] firstObject];
NSString *filestr0=[self autoCodeWithJsonDict:jsonDict modelKey:key];
NSString *filestr1=[self.outPutTextView.string stringByAppendingString:@"\n\n"];
NSString *filestr =[filestr1 stringByAppendingString:filestr0];
[self.outPutTextView setString:filestr];
}
self.arrayModel=nil;
}


-(NSString *)autoCodeWithJsonDict:(NSDictionary *)dic modelKey:(NSString *)classKey
{
if (classKey.length==0||classKey==nil) {
classKey=@"customModel";
}
NSArray *keyArray = [dic allKeys];
NSString *fileStr=[NSString stringWithFormat:@"@interface %@ : NSObject \r\n",classKey];
for(int i=0;i<keyArray.count;i++)
{
NSString *key = [keyArray objectAtIndex:i];
NSLog(@"%@", key);
id value = [dic objectForKey:key];

if([value isKindOfClass:[NSString class]])
{
NSLog(@"string");
fileStr = [NSString stringWithFormat:@"%@@property (strong,nonatomic) NSString *%@;\r\n",fileStr,key];
}
else if([value isKindOfClass:[NSNumber class]])
{
NSLog(@"int");
fileStr = [NSString stringWithFormat:@"%@@property (strong,nonatomic) NSNumber *%@;\r\n",fileStr,key];
}
else if([value isKindOfClass:[NSArray class]])
{
NSLog(@"array");
fileStr = [NSString stringWithFormat:@"%@@property (strong,nonatomic) NSArray *%@;\r\n",fileStr,key];
//判断是否为字典数组
id subvalue=[value lastObject];
if ([subvalue isKindOfClass:[NSDictionary class]]) {
[self.arrayModel addObject:key];
}
}
else
{
NSLog(@"string");
fileStr = [NSString stringWithFormat:@"%@@property (strong,nonatomic) NSString *%@;\r\n",fileStr,key];
}
}

fileStr = [fileStr stringByAppendingString:@"@end"];
return fileStr;
}


-(id)objectFromJSONString:(NSString *)jsonString{
jsonString = [[jsonString stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"jsonString=%@",jsonString);
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
id jsondict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if (err) {
return nil;
}else{
return jsondict;
}
}

- (NSMutableArray *)arrayModel {
if(_arrayModel == nil) {
_arrayModel = [[NSMutableArray alloc] init];
}
return _arrayModel;
}
@end

UC20170406_140411

​ 大致原理就是根据value值判断数据类型,然后进行字符串拼接,加入\r,\n,\t等格式控制符,优化输出模型格式。如果需要需要生产文件,直接将结果写入文件即可。

​ 当然其它功能代码自动生成原理基本一样。

升级优化:

如果需要高亮或显示行号,可以直接使用如JSDCocoaDemos等第三方库,优化显示样式。

文章作者: kyren
文章链接: http://huluo666.github.io/2017/04/06/iOS百行代码打造JOSN模型代码生成器/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kyren's Blog