Skip to content

Commit

Permalink
feat:update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven committed Mar 5, 2021
1 parent 3aaf279 commit 22748c1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
SwiftJSONModeler是一个Xcode插件,一键转换json字符串Swfit模型,一键转化 YApi 平台接口为模型,并且自动引入注释。
SwiftJSONModeler是一个Xcode插件,一键转换json字符串为Swfit模型,一键转化 YApi 平台接口为模型,并且自动引入注释。
* 支持struct, class
* 支持单json转模, 多层嵌套 json
* 支持YApi RAW或接口id解析转模,并且自动引入 YApi 平台注释和兼容数据类型
* 支持自定义遵循 和 import
* 支持自定义模型前缀和后缀
* 可设置隐式和显示可选类型,默认显示可选`?`(不使用则为隐式可选`!`)
* YApi 支持按照自定义路径解析模型,自动解析子类型
* 导入导出配置plist配置,以便团队共享。


## json 转Swfit模型

复制单json, 一步转为模型
复制json字符串, 一步转为Swift模型,支持多层嵌套自动识别

![运行效果](./Sources/example.gif)

Expand Down Expand Up @@ -97,7 +98,7 @@ struct HKSubJsonModel: HandyJSON {

如果无法预览查看[Source/YApiRAW.gif](./Sources/YApiRAW.gif)

如果你使用YApi接口平台,我们支持两种方式,通过接口 Id)和 Raw 数据转模。一键转为模型,并且自动根据YApi为模型引入注释。
如果你使用YApi接口平台,我们支持两种方式,通过接口 Id和 Raw 数据转模。一键转为模型,并且自动根据YApi为模型引入注释。

### 通过Id转模

Expand Down Expand Up @@ -144,7 +145,10 @@ struct HKSubJsonModel: HandyJSON {

再将应用移动到应用程序, 重启Xcode即可使用。

> Tip:如果重启Xcode之后在Editor中还没看见插件选项,请选择系统设置-> 扩展->Xcode Source Editor中对应插件是否导入
### 注意事项

1. 安装弹出安全提示,选择系统设置 -> 安全性与隐私 -> 通用 点击允许打开该软件即可。
2. 如果重启Xcode之后在Editor中还没看见插件选项,请选择系统设置-> 扩展->Xcode Source Editor中对应插件是否勾选

## 设置
SwiftJSONModeler提供多种自定义可选设置,可通过插件的Config选项进行设置。
Expand All @@ -169,6 +173,16 @@ SwiftJSONModeler提供多种自定义可选设置,可通过插件的Config选

![](./Sources/mutilToken.png)

> 多 token 接口无法转模问题
>
> 通过 id 转模,配置多个 token 项目,可能会存在某个项目无法通过 id 砖模型,原因 YApi 平台的 token 无法获取到相应的json数据.
>
> 解决办法: 使用其他项目 token 尝试
设置通过 plist 文件保存,可以导入导出。

> Tips: plist 文件还有其他未展示的设置, 比如默认设置 String 类型为空字符串等
## 设置快捷键

可以给插件设置快捷键,快速转换模型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,69 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "9F6A3FF9-2096-496B-8998-5DF112BBB5C9"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftJSONModeler For Xcode/YApi/YApiHelper.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "56"
endingLineNumber = "56"
landmarkName = "object(from:dic:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "4EE8686A-9525-4D85-BBD2-7C358537B8EC"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftJSONModeler For Xcode/YApi/YApiHelper.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "42"
endingLineNumber = "42"
landmarkName = "check(from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "7AAA994E-CF13-4368-AE2D-9112BFA27732"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftJSONModeler For Xcode/YApi/YApiHelper.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "101"
endingLineNumber = "101"
landmarkName = "getPathObjects(path:from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "72921868-82CF-4D8E-9A98-41E44BC2BE8A"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftJSONModeler For Xcode/YApi/YApiHelper.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "95"
endingLineNumber = "95"
landmarkName = "getPathObjects(path:from:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
6 changes: 4 additions & 2 deletions SwiftJSONModeler For Xcode/YApi/YApiCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class YApiCreator {
if yapiObject.key != nil {
name = yapiObject.key!.upperCaseFirst()
}
let des: String = yapiObject.des ?? "<#描述#>"
let des: String = yapiObject.des ?? ""
var keyword = keyStruct
if commandIdentifier.contains(keyStruct) {
keyword = keyStruct
Expand All @@ -174,7 +174,9 @@ class YApiCreator {
name = config.prefix + name + config.subffix
let parent = config.parent
var objctLines: [String] = []
objctLines.append("/// \(des)")
if !des.isEmpty {
objctLines.append("/// \(des)")
}
if parent.isEmpty {
objctLines.append("\(keyword) \(name) {")
} else {
Expand Down
13 changes: 11 additions & 2 deletions SwiftJSONModeler For Xcode/YApi/YApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class YApiRequest {
var request = URLRequest(url: URL(string: url)!, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

print(request)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: request) { (data, response, error) in
if error != nil {
Expand All @@ -74,7 +74,16 @@ class YApiRequest {
let dataStr = String(data: data, encoding: .utf8)
print("____dataStr")
print(dataStr)
guard let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any], let code = json["errcode"] as? Int, code == 0, let dataDic = json["data"] as? [String: Any], let resBody = dataDic["res_body"] as? String else {
guard let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
errorCenter.message = "获取数据 json 解析异常"
return nil
}
guard let code = json["errcode"] as? Int, code == 0, let dataDic = json["data"] as? [String: Any], let resBody = dataDic["res_body"] as? String else {
var error = "获取接口数据异常"
if let message = json["errmsg"] as? String {
error = message
}
errorCenter.message = error
return nil
}
let raw = resBody.replacingOccurrences(of: #"\""#, with: "\"")
Expand Down

0 comments on commit 22748c1

Please sign in to comment.