ios swift5 codable字典转模型,第三方库SmartCodable

文章目录

  • [1.用第三方库SmartCodable, github地址](#1.用第三方库SmartCodable, github地址)
  • 2.使用示例

1.用第三方库SmartCodable, github地址

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)")
                        }

参考博客:
SmartCodable - Swift数据解析的智能解决方案 简书

相关推荐
00后程序员张7 小时前
iOS 抓不到包怎么办?从 HTTPS 解密、QUIC 排查到 TCP 数据流分析的完整解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
Haha_bj9 小时前
Swift UI数据存储
ios
Zender Han19 小时前
Flutter 新版 Google Sign-In 插件完整解析(含示例讲解)
android·flutter·ios·web
Digitally20 小时前
如何在iPhone上共享日历
ios·iphone
库奇噜啦呼1 天前
【iOS】多线程学习
macos·ios·cocoa
xiAo_Ju1 天前
iOS一个Fancy UI的Tricky实现
前端·ios
游戏开发爱好者81 天前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
QuantumLeap丶1 天前
《Flutter全栈开发实战指南:从零到高级》- 18 -自定义绘制与画布
android·flutter·ios
玲珑Felone1 天前
flutter 状态管理--InheritedWidget、Provider原理解析
android·flutter·ios
AskHarries1 天前
中国身份证注册美区 Apple Developer 个人账号完整教程
ios·apple