@@ -26,41 +26,33 @@ typealias BuildComplete = (NSMutableString, NSMutableString) -> ()
26
26
typealias GenerateFileComplete = ( Bool , String ) -> ( )
27
27
28
28
class SKCodeBuilder : NSObject {
29
-
30
29
var config = SKCodeBuilderConfig ( )
31
30
lazy var handleDicts = NSMutableDictionary ( )
32
31
lazy var yymodelPropertyGenericClassDicts = NSMutableDictionary ( )
33
32
lazy var handlePropertyMapper = NSMutableDictionary ( )
34
33
lazy var allKeys = [ String] ( )
35
-
34
+ var commentDicts : [ String : String ] ? // 适配json文件的注释
35
+
36
36
var fileTye : String {
37
37
get {
38
- if config. codeType == . Swift {
39
- return " swift "
40
- }
38
+ if config. codeType == . Swift { return " swift " }
41
39
return " h "
42
40
}
43
41
}
44
-
45
42
// MARK: - Public
46
-
47
43
func generateCode( with jsonObj: Any , complete: BuildComplete ? ) {
48
44
49
45
allKeys. removeAll ( )
50
-
51
46
let hString = NSMutableString ( )
52
47
let mString = NSMutableString ( )
53
-
54
48
handleDictValue ( dictValue: jsonObj, key: " " , hString: hString, mString: mString)
55
-
56
49
if config. codeType == . OC {
57
50
if config. superClassName == " NSObject " {
58
51
hString. insert ( " \n #import <Foundation/Foundation.h> \n \n " , at: 0 )
59
52
} else {
60
53
hString. insert ( " \n #import \" \( config. superClassName) .h \" \n \n " , at: 0 )
61
54
}
62
55
mString. insert ( " \n #import \" \( config. rootModelName) .h \" \n \n " , at: 0 )
63
-
64
56
} else if config. codeType == . Swift {
65
57
if ( config. jsonType == . HandyJSON) {
66
58
hString. insert ( " import HandyJSON \n \n " , at: 0 )
@@ -76,27 +68,26 @@ class SKCodeBuilder: NSObject {
76
68
"""
77
69
//
78
70
// \( config. rootModelName) . \( fileTye)
79
- // SKCodeBuilder
71
+ // SKGenerateModelTool
80
72
//
81
73
// Created by \( config. authorName) on \( time) .
82
- // Copyright © \( year) SKCodeBuilder . All rights reserved.
74
+ // Copyright © \( year) SKGenerateModelTool . All rights reserved.
83
75
// \n
84
76
"""
85
77
86
78
let mCommentString =
87
79
"""
88
80
//
89
81
// \( config. rootModelName) .m
90
- // SKCodeBuilder
82
+ // SKGenerateModelTool
91
83
//
92
84
// Created by \( config. authorName) on \( time) .
93
- // Copyright © \( year) SKCodeBuilder . All rights reserved.
85
+ // Copyright © \( year) SKGenerateModelTool . All rights reserved.
94
86
// \n
95
87
"""
96
88
97
89
hString. insert ( hCommentString, at: 0 )
98
90
mString. insert ( mCommentString, at: 0 )
99
-
100
91
if let handler = complete {
101
92
handler ( hString, mString)
102
93
}
@@ -107,7 +98,6 @@ class SKCodeBuilder: NSObject {
107
98
108
99
var filePath = filePath
109
100
var success = false
110
-
111
101
if filePath == nil {
112
102
if let desktopPath = NSSearchPathForDirectoriesInDomains ( . desktopDirectory, . userDomainMask, false ) . last {
113
103
let path = desktopPath. appending ( " /SKGenerateModelToolFiles " )
@@ -129,15 +119,13 @@ class SKCodeBuilder: NSObject {
129
119
}
130
120
131
121
var fileNameH = " " , fileNameM = " "
132
-
133
122
if let filePath = filePath {
134
123
if config. codeType == . OC {
135
124
fileNameH = filePath. appending ( " / \( config. rootModelName) .h " )
136
125
fileNameM = filePath. appending ( " / \( config. rootModelName) .m " )
137
126
} else if config. codeType == . Swift {
138
127
fileNameH = filePath. appending ( " / \( config. rootModelName) .swift " )
139
128
}
140
-
141
129
do {
142
130
if !fileNameH. isBlank {
143
131
try hString. write ( toFile: fileNameH, atomically: true , encoding: String . Encoding. utf8. rawValue)
@@ -149,11 +137,7 @@ class SKCodeBuilder: NSObject {
149
137
} catch {
150
138
success = false
151
139
}
152
-
153
- print ( " fileNameH = \( fileNameH) " )
154
- print ( " fileNameM = \( fileNameM) " )
155
140
}
156
-
157
141
if let complete = complete {
158
142
complete ( success, filePath!)
159
143
}
@@ -168,7 +152,6 @@ class SKCodeBuilder: NSObject {
168
152
if key. isBlank { // Root model
169
153
hString. append ( " \n @interface \( config. rootModelName) : \( config. superClassName) \n \n " )
170
154
mString. append ( " \n @implementation \( config. rootModelName) \n \n " )
171
-
172
155
} else { // sub model
173
156
let modelName = modelClassName ( with: key)
174
157
hString. insert ( " @class \( modelName) ; \n " , at: 0 )
@@ -178,7 +161,6 @@ class SKCodeBuilder: NSObject {
178
161
} else if config. codeType == . Swift {
179
162
if key. isBlank { // Root model
180
163
hString. append ( " \n class \( config. rootModelName) : \( config. superClassName) \n \n " )
181
-
182
164
} else { // sub model
183
165
let modelName = modelClassName ( with: key)
184
166
hString. append ( " \n \n class \( modelName) : \( config. superClassName) { \n \n " )
@@ -212,7 +194,6 @@ class SKCodeBuilder: NSObject {
212
194
if config. codeType == . OC {
213
195
hString. append ( " /** \(key) */\n @property (nonatomic, strong) \( modelName) * \( key) ; \n " )
214
196
self . yymodelPropertyGenericClassDicts. setValue ( modelName, forKey: key)
215
-
216
197
} else if config. codeType == . Swift {
217
198
hString. append ( " /// \n var \( key) : \( modelName) ? \n " )
218
199
}
@@ -225,7 +206,7 @@ class SKCodeBuilder: NSObject {
225
206
default :
226
207
// 识别不出类型
227
208
if config. codeType == . OC {
228
- hString. append ( " /** <#识别不出类型 #> */\n @property (nonatomic, strong) id \( key) ; \n " )
209
+ hString. append ( " /** <#泛型 #> */\n @property (nonatomic, strong) id \( key) ; \n " )
229
210
} else if config. codeType == . Swift {
230
211
hString. append ( " /// \( key) \n var \( key) : Any? \n " )
231
212
}
@@ -245,17 +226,14 @@ class SKCodeBuilder: NSObject {
245
226
if config. codeType == . OC {
246
227
hString. append ( " \n @end \n \n " )
247
228
handleJsonType ( hString: hString, mString: mString)
248
-
249
229
} else if config. codeType == . Swift {
250
230
handleJsonType ( hString: hString, mString: mString)
251
231
hString. append ( " } \n " )
252
232
}
253
-
254
233
if !key. isBlank {
255
234
self . handleDicts. removeObject ( forKey: key)
256
235
}
257
236
mString. append ( " \n @end \n \n " )
258
-
259
237
self . yymodelPropertyGenericClassDicts. removeAllObjects ( )
260
238
self . handlePropertyMapper. removeAllObjects ( )
261
239
if self . handleDicts. count > 0 {
@@ -271,54 +249,47 @@ class SKCodeBuilder: NSObject {
271
249
guard arrayValue. count > 0 else {
272
250
return
273
251
}
274
-
275
252
if config. codeType == . OC {
276
-
277
253
if let firstObject = arrayValue. first {
278
-
279
254
if firstObject is String {
280
255
// String 类型
281
- hString. append ( " /** \(key) */\n @property (nonatomic, strong) NSArray <NSString *> * \( key) ; \n " )
256
+ hString. append ( " /** \(commentName( key, firstObject as! String) ) */\n @property (nonatomic, strong) NSArray <NSString *> * \( key) ; \n " )
282
257
}
283
258
else if ( firstObject is [ String : Any ] ) {
284
259
// Dictionary 类型
285
260
let key = handleMaybeSameKey ( key)
286
-
287
261
let modeName = modelClassName ( with: key)
288
262
self . handleDicts. setValue ( firstObject, forKey: key)
289
263
self . yymodelPropertyGenericClassDicts. setValue ( modeName, forKey: key)
290
- hString. append ( " /** \(key) */\n @property (nonatomic, strong) NSArray < \( modeName) *> * \( key) ; \n " )
264
+ hString. append ( " /** \(commentName( key, key) ) */\n @property (nonatomic, strong) NSArray < \( modeName) *> * \( key) ; \n " )
291
265
}
292
266
else if ( firstObject is [ Any ] ) {
293
267
// Array 类型
294
268
handleArrayValue ( arrayValue: firstObject as! [ Any ] , key: key, hString: hString)
295
269
}
296
270
else {
297
- hString. append ( " /** \(key) */\n @property (nonatomic, strong) NSArray * \( key) ; \n " )
271
+ hString. append ( " /** \(commentName( key, key) ) */\n @property (nonatomic, strong) NSArray * \( key) ; \n " )
298
272
}
299
273
}
300
-
301
274
} else if config. codeType == . Swift {
302
-
303
275
if let firstObject = arrayValue. first {
304
-
305
276
if firstObject is String {
306
277
// String 类型
307
- hString. append ( " /// \n var \( key) : [String]? \n " )
278
+ hString. append ( " /// \( commentName ( key , firstObject as! String , false ) ) \ n var \( key) : [String]? \n " )
308
279
}
309
280
else if ( firstObject is [ String : Any ] ) {
310
281
// Dictionary 类型
311
282
let key = handleMaybeSameKey ( key)
312
283
let modeName = modelClassName ( with: key)
313
284
self . handleDicts. setValue ( firstObject, forKey: key)
314
- hString. append ( " /// \n var \( key) : [ \( modeName) ]? \n " )
285
+ hString. append ( " /// \( commentName ( key , key , false ) ) \n var \( key) : [ \( modeName) ]? \n " )
315
286
}
316
287
else if ( firstObject is [ Any ] ) {
317
288
// Array 类型
318
289
handleArrayValue ( arrayValue: firstObject as! [ Any ] , key: key, hString: hString)
319
290
}
320
291
else {
321
- hString. append ( " /// \n var \( key) : [Any]? \n " )
292
+ hString. append ( " /// \( commentName ( key , key , false ) ) \ n var \( key) : [Any]? \n " )
322
293
}
323
294
}
324
295
}
@@ -335,18 +306,18 @@ class SKCodeBuilder: NSObject {
335
306
case . doubleType, . floatType, . float32Type, . float64Type, . cgFloatType:
336
307
/// 浮点型
337
308
if config. codeType == . OC {
338
- hString. append ( " /** eg. \(numValue) */\n @property (nonatomic, assign) CGFloat \( key) ; \n " )
309
+ hString. append ( " /** eg. \(commentName(key, "\( numValue)") ) */\n @property (nonatomic, assign) CGFloat \( key) ; \n " )
339
310
} else if config. codeType == . Swift {
340
- hString. append ( " /// \( numValue) \n var \( key) : Double? \n " )
311
+ hString. append ( " /// \( commentName ( key , " \( numValue) " ) ) \n var \( key) : Double? \n " )
341
312
}
342
313
343
314
case . charType:
344
315
if numValue. int32Value == 0 || numValue. int32Value == 1 {
345
316
/// Bool 类型
346
317
if config. codeType == . OC {
347
- hString. append ( " /** eg. \(numValue) */\n @property (nonatomic, assign) BOOL \( key) ; \n " )
318
+ hString. append ( " /** eg. \(commentName(key, "\( numValue)") ) */\n @property (nonatomic, assign) BOOL \( key) ; \n " )
348
319
} else if config. codeType == . Swift {
349
- hString. append ( " /// \( numValue. boolValue == true ? " true " : " false " ) \n var \( key) : Bool = false \n " )
320
+ hString. append ( " /// \( commentName ( key , ( numValue. boolValue == true ? " true " : " false " ) ) ) \n var \( key) : Bool = false \n " )
350
321
}
351
322
} else {
352
323
handleIdStringValue ( idValue: numValue. stringValue, key: key, hString: hString, ignoreIdValue: ignoreIdValue)
@@ -364,18 +335,19 @@ class SKCodeBuilder: NSObject {
364
335
365
336
private func handleIdIntValue( intValue: Int , key: String , hString: NSMutableString , ignoreIdValue: Bool ) {
366
337
/// Int
338
+ let comment = ( commentName ( key, " \( intValue) " ) )
367
339
if key == " id " && !ignoreIdValue {
368
340
self . handlePropertyMapper. setValue ( " id " , forKey: " itemId " )
369
341
if config. codeType == . OC {
370
- hString. append ( " /** eg. \(intValue ) */\n @property (nonatomic, assign) NSInteger itemId; \n " )
342
+ hString. append ( " /** eg. \(comment ) */\n @property (nonatomic, assign) NSInteger itemId; \n " )
371
343
} else if config. codeType == . Swift {
372
- hString. append ( " /// \( intValue ) \n var itemId: Int = 0 \n " )
344
+ hString. append ( " /// \( comment ) \n var itemId: Int = 0 \n " )
373
345
}
374
346
} else {
375
347
if config. codeType == . OC {
376
- hString. append ( " /** eg. \(intValue ) */\n @property (nonatomic, assign) NSInteger \( key) ; \n " )
348
+ hString. append ( " /** eg. \(comment ) */\n @property (nonatomic, assign) NSInteger \( key) ; \n " )
377
349
} else if config. codeType == . Swift {
378
- hString. append ( " /// \( intValue ) \n var \( key) : Int = 0 \n " )
350
+ hString. append ( " /// \( comment ) \n var \( key) : Int = 0 \n " )
379
351
}
380
352
}
381
353
}
@@ -388,28 +360,25 @@ class SKCodeBuilder: NSObject {
388
360
if key == " id " && !ignoreIdValue {
389
361
// 字符串id 替换成 itemId
390
362
self . handlePropertyMapper. setValue ( " id " , forKey: " itemId " )
391
- hString. append ( " /** eg. \(idValue) */\n @property (nonatomic, copy) NSString *itemId; \n " )
363
+ hString. append ( " /** eg. \(commentName(key, idValue) ) */\n @property (nonatomic, copy) NSString *itemId; \n " )
392
364
} else {
393
- if idValue. count > 12 {
394
- hString. append ( " /** eg. \(key) */\n @property (nonatomic, copy) NSString * \( key) ; \n " )
395
- } else {
396
- hString. append ( " /** eg. \(idValue) */\n @property (nonatomic, copy) NSString * \( key) ; \n " )
397
- }
365
+ hString. append ( " /** eg. \(commentName(key, idValue)) */\n @property (nonatomic, copy) NSString * \( key) ; \n " )
398
366
}
399
367
} else if config. codeType == . Swift {
400
368
if key == " id " && !ignoreIdValue {
401
369
self . handlePropertyMapper. setValue ( " id " , forKey: " itemId " )
402
- hString. append ( " /// \( idValue) \n var itemId: String? \n " )
370
+ hString. append ( " /// \( commentName ( key , idValue) ) \n var itemId: String? \n " )
403
371
} else {
404
372
if idValue. count > 12 {
405
- hString. append ( " /// \n var \( key) : String? \n " )
373
+ hString. append ( " /// \( commentName ( key , idValue , false ) ) \ n var \( key) : String? \n " )
406
374
} else {
407
- hString. append ( " /// \( idValue) \n var \( key) : String? \n " )
375
+ hString. append ( " /// \( commentName ( key , idValue) ) \n var \( key) : String? \n " )
408
376
}
409
377
}
410
378
}
411
379
}
412
380
381
+
413
382
/// 处理json解析
414
383
415
384
private func handleJsonType( hString: NSMutableString , mString: NSMutableString ) {
@@ -444,9 +413,7 @@ class SKCodeBuilder: NSObject {
444
413
445
414
/// 2.Custom property mapper.
446
415
if ( self . handlePropertyMapper. count > 0 ) {
447
- if ( needLineBreak) {
448
- mString. append ( " \n " )
449
- }
416
+ if ( needLineBreak) { mString. append ( " \n " ) }
450
417
mString. append ( " + (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper \n " )
451
418
mString. append ( " { \n return @{ \n " )
452
419
for (key, obj) in self . handlePropertyMapper {
@@ -471,9 +438,7 @@ class SKCodeBuilder: NSObject {
471
438
}
472
439
473
440
if ( self . handlePropertyMapper. count > 0 ) {
474
- if ( needLineBreak) {
475
- mString. append ( " \n " )
476
- }
441
+ if ( needLineBreak) { mString. append ( " \n " ) }
477
442
mString. append ( " + (NSDictionary *)mj_replacedKeyFromPropertyName \n " )
478
443
mString. append ( " { \n return @{ \n " )
479
444
for (key, obj) in self . handlePropertyMapper {
@@ -537,6 +502,21 @@ class SKCodeBuilder: NSObject {
537
502
}
538
503
return modelName
539
504
}
505
+
506
+ /// 生成注释
507
+ private func commentName( _ key: String , _ value: String , _ show: Bool = true ) -> String {
508
+ var comment = value
509
+ if value. count > 12 {
510
+ comment = key
511
+ if !show { comment = " " }
512
+ }
513
+ if let commentDict = commentDicts, commentDict. count > 0 {
514
+ if let commentValue = commentDict [ key] {
515
+ comment = commentValue
516
+ }
517
+ }
518
+ return comment
519
+ }
540
520
}
541
521
542
522
// MARK: - Config
@@ -591,6 +571,17 @@ extension String {
591
571
return NSMakeRange ( start, end - start)
592
572
}
593
573
574
+ /// 在字符串中查找另一字符串首次出现的位置(或最后一次出现位置)
575
+ func postionOf( sub: String , backwards: Bool = false ) -> Int {
576
+ var pos = - 1
577
+ if let range = range ( of: sub, options: backwards ? . backwards : . literal, range: nil , locale: nil ) {
578
+ if !range. isEmpty {
579
+ pos = self . distance ( from: startIndex, to: range. lowerBound)
580
+ }
581
+ }
582
+ return pos
583
+ }
584
+
594
585
/// url 编码
595
586
func _adler32( ) -> String {
596
587
if self . isBlank { return self }
0 commit comments