Skip to content

Commit 17f1e19

Browse files
committed
Compatible with YYModel.
1 parent 37d30bf commit 17f1e19

File tree

3 files changed

+55
-140
lines changed

3 files changed

+55
-140
lines changed

README.md

Lines changed: 31 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -5,141 +5,55 @@
55
![](https://img.shields.io/badge/Swift-91.7%25-brightgreen)
66
![](https://img.shields.io/badge/license-MIT-green)
77

8-
- 支持Objective-C / Swift / Dart / TypeScript
9-
- 支持通过URL链接或json字符串一键生成model文件
10-
- 兼容YYModel / MJExtension / HandyJSON解析
11-
- 支持字符串加密(可设置不同的密钥,开发者可自行修改加密算法)
12-
- 支持自定义model父类名、自定义model前缀、自定义作者名、添加注释
13-
- 支持代码高亮(使用[highlight.js](https://highlightjs.org/)
14-
- 支持导出TypeScript Interfaces
15-
- 支持自定义导出文件路径
16-
- Flutter Model解析兼容了服务端返回string/int/double时数据类型混用的情况
17-
- 兼容服务端返回“id”字段
18-
- 支持类驼峰命名
19-
20-
- Support OC / Swift / Dart / TypeScript
21-
- Supports one-click generation of model files through URL links or json strings
22-
- Compatible with YYModel / MJExtension analysis
8+
9+
- [x] 支持**Objective-C** / **Swift** / **Dart** / **TypeScript**等多种语言
10+
- [x] 支持通过`URL`链接或`json`字符串一键生成model文件
11+
- [x] 兼容`YYModel` / `MJExtension` / `HandyJSON`解析
12+
- [x] 支持字符串加密(可设置不同的密钥,开发者可自行修改加密算法)
13+
- [x] 支持自定义model父类名、自定义model前缀、自定义作者名、添加注释
14+
- [x] 支持代码高亮(使用[highlight.js](https://highlightjs.org/)
15+
- [x] 支持导出`TypeScript Interfaces`
16+
- [x] 支持自定义导出文件路径
17+
- [x] `Flutter Model`解析兼容了服务端返回`string/int/double`时数据类型混用的情况
18+
- [x] 兼容服务端返回“id”字段
19+
- [x] 支持类驼峰命名
20+
21+
- Support **Objective-C** / **Swift** / **Dart** / **TypeScript**
22+
- Supports one-click generation of model files through `URL` links or `json` strings
23+
- Compatible with `YYModel` / `MJExtension` / `HandyJSON` analysis
2324
- Support string encryption (different keys can be set, developers can modify the encryption algorithm)
2425
- Support custom model superclass name, custom model prefix, custom author name, add comments
2526
- Support code highlighting(It uses [highlight.js](https://highlightjs.org/) as it core)
26-
- Support for exporting TypeScript Interfaces
27+
- Support for exporting `TypeScript Interfaces`
2728
- Support custom output folder path
28-
- Flutter model parsing is compatible with mixed data types when the server returns string / int / double
29+
- `Flutter model` parsing is compatible with mixed data types when the server returns string / int / double
2930
- Compatible server returns "id" field
3031
- Supports hump naming
3132

33+
### Usage
3234

33-
![SKGenerateModelTool](../master/images/home.png)
35+
> * 方式一:直接下载软件安装 [![](https://img.shields.io/badge/.dmg-4.2MB-brightgreen)](https://github.com/Xcoder1011/SKGenerateModelTool/blob/master/SKGenerateModelTool.dmg)
36+
> * 方式二:下载代码运行
3437
3538

36-
###### 生成Flutter Dart Model
39+
### Objective-C
3740

38-
```
39-
//
40-
// news_response.dart
41-
// SKGenerateModelTool
42-
//
43-
// Created by wushangkun on 2021/01/29.
44-
// Copyright © 2021 SKGenerateModelTool. All rights reserved.
45-
//
46-
47-
part 'news_response.m.dart';
48-
49-
class NewsResponse {
50-
List<SKDataModel> data;
51-
String msg; // success
52-
int code; // 200
53-
54-
NewsResponse fromJson(Map<String, dynamic> json) => _$NewsResponseFromJson(json, this);
55-
Map<String, dynamic> toJson() => _$NewsResponseToJson(this);
56-
}
57-
58-
class SKDataModel {
59-
String title;
60-
String source; // 环球网资讯
61-
String imgsrc;
62-
63-
SKDataModel fromJson(Map<String, dynamic> json) => _$SKDataModelFromJson(json, this);
64-
Map<String, dynamic> toJson() => _$SKDataModelToJson(this);
65-
}
41+
![Objective-C](../master/images/home.png)
6642

67-
```
43+
### Swift
6844

69-
实现文件
45+
![Swift](../master/images/swift.png)
7046

71-
```
72-
//
73-
// news_response.m.dart
74-
// SKGenerateModelTool
75-
//
76-
// Created by wushangkun on 2021/01/29.
77-
// Copyright © 2021 SKGenerateModelTool. All rights reserved.
78-
//
79-
80-
part of 'news_response.dart';
81-
82-
NewsResponse _$NewsResponseFromJson(Map<String, dynamic> json, NewsResponse instance) {
83-
if(json['data'] != null) {
84-
instance.data = new List<SKDataModel>();
85-
(json['data'] as List).forEach((v) {
86-
instance.data.add(new SKDataModel().fromJson(v));
87-
});
88-
}
89-
if(json['msg'] != null) {
90-
instance.msg = json['msg']?.toString();
91-
}
92-
if(json['code'] != null) {
93-
final code = json['code'];
94-
if(code is String) {
95-
instance.code = int.parse(code);
96-
} else {
97-
instance.code = code?.toInt();
98-
}
99-
}
100-
return instance;
101-
}
102-
103-
Map<String, dynamic> _$NewsResponseToJson(NewsResponse instance) {
104-
final Map<String, dynamic> json = new Map<String, dynamic>();
105-
if(instance.data != null) {
106-
json['data'] = instance.data.map((v) => v.toJson()).toList();
107-
}
108-
json['msg'] = instance.msg;
109-
json['code'] = instance.code;
110-
return json;
111-
}
112-
113-
SKDataModel _$SKDataModelFromJson(Map<String, dynamic> json, SKDataModel instance) {
114-
if(json['title'] != null) {
115-
instance.title = json['title']?.toString();
116-
}
117-
if(json['source'] != null) {
118-
instance.source = json['source']?.toString();
119-
}
120-
if(json['imgsrc'] != null) {
121-
instance.imgsrc = json['imgsrc']?.toString();
122-
}
123-
return instance;
124-
}
125-
126-
Map<String, dynamic> _$SKDataModelToJson(SKDataModel instance) {
127-
final Map<String, dynamic> json = new Map<String, dynamic>();
128-
json['title'] = instance.title;
129-
json['source'] = instance.source;
130-
json['imgsrc'] = instance.imgsrc;
131-
return json;
132-
}
47+
### Dart
13348

134-
```
49+
![Dart](../master/images/dart.png)
50+
51+
### TypeScript Interfaces
13552

136-
###### Flutter Model header
137-
![header](../master/images/flutter1.png)
53+
![TypeScript](../master/images/typescript.png)
13854

139-
###### TypeScript Interfaces
140-
![header](../master/images/typescript.png)
55+
### 字符串加密示例
14156

142-
###### 字符串加密示例
14357
![字符串加密示例](../master/images/encrypt.png)
14458

14559
加密后的内容添加到项目中(声明和定义可以分别放.h和.m),因为代码依赖SKEncryptString结构体,所以需要导入头文件**SKEncryptHeader.h**引用。
@@ -206,5 +120,3 @@ const SKEncryptString * const _3908173925 = &(SKEncryptString){
206120
Tip:本工具仅用到简单的XOR加密算法,开发者可自行下载项目进行加密算法修改,另外也可直接下载项目里的dmg文件进行安装使用。
207121

208122
- 简书地址:[https://www.jianshu.com/p/a2ee31a04252](https://www.jianshu.com/p/a2ee31a04252)
209-
210-

SKGenerateModelTool/SKCodeBuilder.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,20 @@ class SKCodeBuilder: NSObject {
9292
handleDictValue(dictValue: jsonObj, key: "", hString: hString, mString: mString)
9393
if config.codeType == .OC {
9494
if config.superClassName == "NSObject" {
95-
hString.insert("\n#import <Foundation/Foundation.h>\n\n", at: 0)
95+
if ((config.jsonType == .YYModel) && (config.superClassName.compare("NSObject") == .orderedSame)) {
96+
let string =
97+
"""
98+
\n#if __has_include(<YYModel/YYModel.h>)
99+
#import <YYModel/YYModel.h>
100+
#else
101+
#import "YYModel.h"
102+
#endif\n\n
103+
"""
104+
hString.insert(string, at: 0)
105+
} else {
106+
hString.insert("\n#import <Foundation/Foundation.h>\n\n", at: 0)
107+
}
108+
96109
} else {
97110
hString.insert("\n#import \"\(config.superClassName).h\"\n\n", at: 0)
98111
}
@@ -205,12 +218,20 @@ class SKCodeBuilder: NSObject {
205218

206219
if config.codeType == .OC {
207220
if key.isBlank { // Root model
208-
hString.append("\n@interface \(config.rootModelName) : \(config.superClassName)\n\n")
221+
if ((config.jsonType == .YYModel) && (config.superClassName.compare("NSObject") == .orderedSame)) {
222+
hString.append("\n@interface \(config.rootModelName) : \(config.superClassName) <YYModel>\n\n")
223+
} else {
224+
hString.append("\n@interface \(config.rootModelName) : \(config.superClassName)\n\n")
225+
}
209226
mString.append("\n@implementation \(config.rootModelName)\n\n")
210227
} else { // sub model
211228
let modelName = modelClassName(with: key)
212229
hString.insert("@class \(modelName);\n", at: 0)
213-
hString.append("\n@interface \(modelName) : \(config.superClassName)\n\n")
230+
if ((config.jsonType == .YYModel) && (config.superClassName.compare("NSObject") == .orderedSame)) {
231+
hString.append("\n@interface \(modelName) : \(config.superClassName) <YYModel>\n\n")
232+
} else {
233+
hString.append("\n@interface \(modelName) : \(config.superClassName)\n\n")
234+
}
214235
mString.append("\n@implementation \(modelName)\n\n")
215236
}
216237
} else if config.codeType == .Swift {

SKGenerateModelTool/ViewController.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,9 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
291291
/// config ui on main queue.
292292

293293
private func configJsonTextView(text:String, textView:NSTextView, color:NSColor) {
294-
295-
// if let highlightr = Highlightr() {
296-
// highlightr.setTheme(to: "paraiso-dark")
297-
// if let attrString = highlightr.highlight(text, as: "swift") {
298-
// DispatchQueue.main.async {
299-
// textView.textStorage?.setAttributedString(attrString)
300-
// textView.textStorage?.font = NSFont.systemFont(ofSize: 15)
301-
// textView.textStorage?.foregroundColor = color
302-
// }
303-
// }
304-
// }
305-
//
306-
307294
let attrString = NSAttributedString(string: text)
308295
DispatchQueue.main.async {
309296
textView.textStorage?.setAttributedString(attrString)
310-
// textView.textStorage?.font = NSFont.systemFont(ofSize: 15)
311-
// textView.textStorage?.foregroundColor = color
312297
textView.textStorage?.foregroundColor = .clear
313298

314299
}
@@ -397,15 +382,12 @@ class ViewController: NSViewController, NSControlTextEditingDelegate {
397382
UserDefaults.standard.setValue(authorName, forKey: AuthorNameCacheKey)
398383
builder.config.authorName = authorName
399384

400-
401385
builder.config.jsonType = SKCodeBuilderJSONModelType(rawValue: jsonTypeBtn.indexOfSelectedItem)!
402386
UserDefaults.standard.set(jsonTypeBtn.indexOfSelectedItem, forKey: SupportJSONModelTypeCacheKey)
403387

404388
if builder.config.superClassName.compare("NSObject") == .orderedSame {
405389
if builder.config.jsonType == .HandyJSON {
406390
builder.config.superClassName = "HandyJSON"
407-
} else if builder.config.jsonType == .YYModel {
408-
builder.config.superClassName = "YYModel"
409391
}
410392
}
411393
UserDefaults.standard.setValue(outputFilePath, forKey: GenerateFilePathCacheKey)

0 commit comments

Comments
 (0)