文章目录
- 1.用第三方库SmartCodable, github地址
- 2.使用示例
1.用第三方库SmartCodable, github地址
SmartCodable - github
2.使用示例
import Foundation
import SmartCodablestruct CommonModel: SmartCodable {var message: String = ""var success: Bool = falsevar code: Int = -1var result: SmartAny? //result可能是字典,也可能是数组var timestamp: Int = -1}
- moya发起网络请求后解析
provider.request(.login(username: email, password: password)) { result inswitch result {case let .success(response):do {// 解析响应数据if let json = try response.mapJSON() as? [String: Any] {guard let commonModel = CommonModel.deserialize(from: json) else { return }guard let dict = commonModel.result?.peel as? [String:Any] else { return }if let token = dict["token"] as? String {MyPrint("Login successful, token: \(token)")}
参考博客:
SmartCodable - Swift数据解析的智能解决方案 简书