This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeModelStrings.m
More file actions
182 lines (126 loc) · 7.51 KB
/
NodeModelStrings.m
File metadata and controls
182 lines (126 loc) · 7.51 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// NodeModelStrings.m
// Create-JSON-Model
//
// Created by YouXianMing on 15/11/11.
// Copyright © 2015年 YouXianMing. All rights reserved.
//
#import "NodeModelStrings.h"
// [FileHeaders-WaitForReplaced]
// [PropertiesList-WaitForReplaced]
@interface NodeModelStrings ()
@property (nonatomic, strong) NSDictionary *nodeModelStringsPlist;
@property (nonatomic, strong) NSString *modelHeaderFileString;
@property (nonatomic, strong) NSString *modelMFileString;
@end
@implementation NodeModelStrings
- (instancetype)init {
if (self = [super init]) {
self.nodeModelStringsPlist = [self accessModelPlist];
self.modelHeaderFileString = self.nodeModelStringsPlist[@"modelHeaderFileString"];
self.modelMFileString = self.nodeModelStringsPlist[@"modelMFileString"];
}
return self;
}
+ (instancetype)nodeModelStringsWithNodeModel:(NodeModel *)nodeModel {
NodeModelStrings *nodeModelStrings = [[self alloc] init];
nodeModelStrings.nodeModel = nodeModel;
return nodeModelStrings;
}
/**
* 获取plist文件
*
* @return 字典
*/
- (NSDictionary *)accessModelPlist {
NSString *path = [[NSBundle mainBundle] pathForResource:@"NodeModelStrings.plist" ofType:nil];
NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:path];
return data;
}
- (void)createFile {
// 替换文件名字
NSString *fileName = self.nodeModel.modelName;
self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[ModelName-WaitForReplaced]"
withString:fileName];
self.modelMFileString = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[ModelName-WaitForReplaced]"
withString:fileName];
// 导入头文件字符串
NSString *inputHeaderString = @"";
for (PropertyInfomation *property in self.nodeModel.properties) {
if (property.propertyType == kNSDictionary || property.propertyType == kNSArray) {
NodeModel *node = property.propertyValue;
NSString *importString = [NSString stringWithFormat:@"#import \"%@.h\"\n", node.modelName];
inputHeaderString = [inputHeaderString stringByAppendingString:importString];
}
}
self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[FileHeaders-WaitForReplaced]"
withString:inputHeaderString];
// 替换头文件属性
NSString *propetiesString = @"";
for (PropertyInfomation *property in self.nodeModel.properties) {
switch (property.propertyType) {
case kNSString: {
NSString *tmpSting = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;\n", property.propertyValue];
propetiesString = [propetiesString stringByAppendingString:tmpSting];
} break;
case kNSNumber: {
NSString *tmpSting = [NSString stringWithFormat:@"@property (nonatomic, strong) NSNumber *%@;\n", property.propertyValue];
propetiesString = [propetiesString stringByAppendingString:tmpSting];
} break;
case kNull: {
NSString *tmpSting = [NSString stringWithFormat:@"// @property (nonatomic, strong) Null *%@;\n", property.propertyValue];
propetiesString = [propetiesString stringByAppendingString:tmpSting];
} break;
case kNSDictionary: {
NodeModel *nodeModel = property.propertyValue;
NSString *tmpSting = [NSString stringWithFormat:@"@property (nonatomic, strong) %@ *%@;\n", nodeModel.modelName, nodeModel.listType];
propetiesString = [propetiesString stringByAppendingString:tmpSting];
} break;
case kNSArray:{
NodeModel *nodeModel = property.propertyValue;
NSString *tmpSting = [NSString stringWithFormat:@"@property (nonatomic, strong) NSMutableArray <%@ *> *%@;\n", nodeModel.modelName, nodeModel.listType];
propetiesString = [propetiesString stringByAppendingString:tmpSting];
} break;
default:
break;
}
}
self.modelHeaderFileString = [self.modelHeaderFileString stringByReplacingOccurrencesOfString:@"[PropertiesList-WaitForReplaced]"
withString:propetiesString];
// 替换实现文件
NSMutableString *mPropertyString = [NSMutableString string];
for (PropertyInfomation *property in self.nodeModel.properties) {
if (property.propertyType == kNSArray) {
NodeModel *nodeModel = property.propertyValue;
NSString *listTypeString = [NSString stringWithFormat:@"%@\n", self.nodeModelStringsPlist[@"arrayTypeString"]];
listTypeString = [listTypeString stringByReplacingOccurrencesOfString:@"[PropertyName]" withString:nodeModel.listType];
listTypeString = [listTypeString stringByReplacingOccurrencesOfString:@"[PropertyClass]" withString:nodeModel.modelName];
[mPropertyString appendString:listTypeString];
}
if (property.propertyType == kNSDictionary) {
NodeModel *nodeModel = property.propertyValue;
NSString *listTypeString = [NSString stringWithFormat:@"%@\n", self.nodeModelStringsPlist[@"dictionaryTypeString"]];
listTypeString = [listTypeString stringByReplacingOccurrencesOfString:@"[PropertyName]" withString:nodeModel.listType];
listTypeString = [listTypeString stringByReplacingOccurrencesOfString:@"[PropertyClass]" withString:nodeModel.modelName];
[mPropertyString appendString:listTypeString];
}
}
self.modelMFileString = [self.modelMFileString stringByReplacingOccurrencesOfString:@"[ListProperties-WaitForReplaced]"
withString:mPropertyString];
[self.modelHeaderFileString writeToFile:[self filePathWithFileName:[self.nodeModel.modelName stringByAppendingString:@".h"]]
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
[self.modelMFileString writeToFile:[self filePathWithFileName:[self.nodeModel.modelName stringByAppendingString:@".m"]]
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
- (void)string:(NSString *)string replaceString:(NSString *)replaceString withString:(NSString *)newString {
[string stringByReplacingOccurrencesOfString:replaceString
withString:newString];
}
- (NSString *)filePathWithFileName:(NSString *)name {
return [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/%@", name]];
}
@end