文章目录
- [1.用第三方库SmartCodable, github地址](#1.用第三方库SmartCodable, github地址)
- 2.使用示例
1.用第三方库SmartCodable, github地址
2.使用示例
swift
import Foundation
import SmartCodable
struct CommonModel: SmartCodable {
var message: String = ""
var success: Bool = false
var code: Int = -1
var result: SmartAny? //result可能是字典,也可能是数组
var timestamp: Int = -1
}
- moya发起网络请求后解析
swift
provider.request(.login(username: email, password: password)) { result in
switch 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)")
}