1
- SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
1
+ SwiftJSONModeler是一个Xcode插件,可以将json 转为Swfit模型
2
2
* 支持struct, class
3
- * 支持标准单json和YApi RAW(自动为你添加注释)
3
+ * 支持单json转模
4
+ * 支持YApi RAW或接口id解析转模,并且自动添加注释
4
5
* 支持自定义遵循 和 import
5
6
* 支持自定义模型前缀和后缀
6
7
* 可设置是否类型可选,是否使用显示可选` ? ` (不使用则为隐式可选` ! ` )
@@ -13,15 +14,16 @@ SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
13
14
复制单json, 一步转为模型。
14
15
15
16
![ 运行效果] ( ./Sources/example.gif )
16
- 如果无法预览查看Sources /example.gif
17
+ 如果无法预览查看 [ Sources /example.gif] ( ./Sources/example.gif )
17
18
18
19
### YApi RAW解析
19
20
20
- 复制YApi接口RAW数据,一步解析模型。如果配置为继承和引入HandyJSON, 添加JD模型前缀和Model后缀,解析效果如下:
21
+ 复制YApi接口RAW数据,一步解析带有注释的模型。
22
+ 解析效果如下():
21
23
22
24
![ 解析YApi] ( ./Sources/YApiRAW.gif )
23
25
24
- 如果无法预览查看Source /YApiRAW.gif
26
+ 如果无法预览查看 [ Source /YApiRAW.gif] ( ./Sources/YApiRAW.gif )
25
27
26
28
## 安装
27
29
1.你可以选择[ Release] ( https://github.com/yumengqing/SwiftJSONModeler/releases ) 直接下载应用安装
@@ -41,29 +43,36 @@ SwiftJSONModeler是一个Xcode插件,可以将json 转为模型
41
43
42
44
> Tip:如果重启Xcode之后在Editor中还没看见插件选项,请选择系统设置-> 扩展->Xcode Source Editor中对应插件是否导入
43
45
44
- ### 设置快捷键
46
+ ## 设置
47
+ SwiftJSONModeler提供多种自定义可选设置,可通过插件的Config选项进行设置。
45
48
46
- 可以给插件设置快捷键,快速转换模型
49
+ 设置选项如下图:
47
50
48
- 在Xcode -> Preference -> Key Bindings -> Editor Menu For Source Code【或者搜索】就可以找到,如下图
51
+ ![ 可选设置 ] ( ./Sources/config.png )
49
52
50
- ![ ] ( ./Sources/keybinding.png )
53
+ ** YApi模型路径指定 ** :当对象含有多层json,或者有基本json包裹时,可以指定path解析模型。
51
54
52
- 记得双击key下面那个区域才可以编辑,这里我使用的是alt + s 和alt + c 避免与系统的冲突
53
- ### Model设置
54
- 模型设置提供一些自定义控制,具体如下图:
55
- ![ Alt ] ( ./Sources/config.png )
56
- ### 自定义操作confrom 和import
55
+ ** YApi项目token ** : 每个YApi项目都有唯一一个token,在YApi项目接口的设置中可以查看对应的token,如下图:
56
+
57
+ ![ token ] ( ./Sources/yapiToken.png )
58
+
59
+ ** YApi项目host ** :host就是部署YApi的地址
57
60
58
- 可以通过点击应用或者选择扩展的config选项,自定义填写import模块和继承
61
+ ![ host ] ( ./Sources/yapiHostId.png )
59
62
60
- 比如我需要继承HandyJSON
63
+ ## 设置快捷键
61
64
62
- ![ 自定义遵循] ( ./Sources/customConfig.png )
65
+ 可以给插件设置快捷键,快速转换模型
66
+
67
+ 在Xcode -> Preference -> Key Bindings -> Editor Menu For Source Code【或者搜索】就可以找到,如下图
68
+
69
+ ![ ] ( ./Sources/keybinding.png )
63
70
71
+ 记得双击key下面那个区域才可以编辑,这里我使用的是alt + s 和alt + c 避免与系统的冲突
64
72
## 标准JSON转模
65
73
66
- 使用的使用,首先要选中需要转换的json,注意不标准的json会报错的哟。仅限单层json,多层json子json需要单独转换
74
+ 复制需要转换的json,选择插件的Struct JSON或者Class JSON功能,即可生成相应Swift模型。
75
+ 注意:目前只支持单层json
67
76
68
77
示例json
69
78
@@ -104,7 +113,17 @@ struct <#Model#>: HandyJSON {
104
113
105
114
## YApi 接口平台转模
106
115
107
- 如果你使用YApi接口平台,可以通过复制接口返回的 RAW数据,自动转为模型,并且自动根据YApi为模型添加注释。
116
+ 如果你使用YApi接口平台,可以通过复制接口返回的 RAW数据,或者复制Id,自动转为模型,并且自动根据YApi为模型添加注释。
117
+
118
+ ### 通过Id转模
119
+
120
+ 在配置了项目token和host基础下,简单复制Id即可实现转模和添加注释.
121
+
122
+ ** 如何查看YApi接口中的id?** 在YApi对应的接口中,查看浏览器网址,最后的数字就是Id。如下图:
123
+
124
+ ![ 接口Id] ( ./Sources/yapiHostId.png )
125
+
126
+
108
127
109
128
### RAW数据
110
129
@@ -114,15 +133,17 @@ struct <#Model#>: HandyJSON {
114
133
115
134
示例RAW数据:
116
135
117
- ``` swift
118
- {" type" : " object" ," title" : " empty object" ," properties" : {" person" : {" type" : " object" ," properties" : {" name" : {" type" : " string" ," description" : " 姓名" ," mock" : {" mock" : " 小明" }}," age" : {" type" : " integer" ," description" : " 年龄" ," mock" : {" mock" : " 12" }}," school" : {" type" : " object" ," properties" : {" address" : {" type" : " string" ," mock" : {" mock" : " xx街道xx号" }," description" : " 学校地址" }," schoolName" : {" type" : " string" ," description" : " 学校名字" }}," required" : [" address" ," schoolName" ]," description" : " 学校" }," likes" : {" type" : " array" ," items" : {" type" : " string" }," description" : " 好友" }," teachers" : {" type" : " array" ," items" : {" type" : " object" ," properties" : {" name" : {" type" : " string" ," description" : " 老师名字" }," subject" : {" type" : " string" ," description" : " 科目" ," mock" : {" mock" : " 语文" }}," isMale" : {" type" : " boolean" ," description" : " 是否为男" }}," required" : [" name" ," subject" ," isMale" ]}}}," required" : [" name" ," age" ," school" ," likes" ," teachers" ]," description" : " 个人信息详情" }}," required" : [" person" ]}
136
+ ``` javascript
137
+ {"type":"object","title":"empty object","properties":{"message":{"type":"string"},"code":{"type":"string"},"response":{"type":"object","properties":{"teachers":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string","mock":{"mock":"Mrs Yang"},"description":"名字"},"subject":{"type":"string","mock":{"mock":"语文"},"description":"科目"},"phone":{"type":"string","mock":{"mock":"13459923098"},"description":"联系电话"}},"required":["name","subject","phone"]},"description":"老师"},"name":{"type":"string","description":"姓名"},"age":{"type":"integer","mock":{"mock":"18"},"description":"年龄"},"score":{"type":"number","mock":{"mock":"89.8"},"description":"综合成绩"},"likes":{"type":"array","items":{"type":"string","mock":{"mock":"英雄联盟"}},"description":"爱好"},"emergercyContact":{"type":"object","properties":{"name":{"type":"string"},"phone":{"type":"string","description":"联系电话"},"address":{"type":"string","description":"联系地址","mock":{"mock":"xx街道xx栋xx单元"}}},"description":"紧急联系人","required":["name","phone","address"]},"isBoy":{"type":"boolean","description":"是否为男孩"}},"required":["teachers","name","age","score","likes","emergercyContact","isBoy"]}},"required":["message","code","response"]}
119
138
```
120
139
121
140
> 完整示例可下载打开SwiftJSONModelerDemo查看
122
141
123
142
### 解析指定路径模型
124
143
125
- 如果你数据有多层json, 可以在设置中指定解析路线path 比如想解析json data属性下的,则在path中添加` data ` ,也可以解析多层路径比如` data.person `
144
+ 如果你数据有多层json, 可以在设置中指定解析路线path来获取指定模型。比如我的目标数据在response字段下,则可以配置path为response.则直接解析response下的json模型。多路径使用 ` . `
145
+
146
+
126
147
127
148
## 待优化
128
149
0 commit comments