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数据解析的智能解决方案 简书

相关推荐
2501_915106328 小时前
苹果App Store上架费用及流程全面解析
android·ios·小程序·https·uni-app·iphone·webview
Privasa-隐私实验室9 小时前
蓝牙-多设备调试效率翻倍:从手动折腾到全自动调试的完整落地流程
物联网·ios·智能家居·智能硬件·智能手表
黑科技iOS上架13 小时前
UnityiOS工程混淆能解决App Store4.3被拒么?
ios·审核
恋猫de小郭14 小时前
iPhone Duo 适配详解,需要改变的不止是布局模型
前端·flutter·ios
Digitally16 小时前
如何在不从 iCloud 删除的情况下从iPhone删除照片
ios·iphone·icloud
游戏开发爱好者81 天前
网络连接排查指南,谁在电脑后台偷跑流量,哪些程序在联网
网络协议·计算机网络·网络安全·ios·adb·https·udp
ii_best1 天前
安卓脚本/ios开发软件按键精灵实战:自动定位弹窗广告关闭按钮坐标的通用方案
android·ios·自动化
Behaviour1 天前
iPhone Duo 来了,苹果 Siri AI 接入定制 Gemini
人工智能·ios·语言模型·aigc·iphone
用户38034165882971 天前
手写 fMP4 muxer:从 ISO 14496-12 到能播的文件
ios·音视频开发
深念Y1 天前
iOS模拟器在无Metal的NVIDIA显卡环境下的可行性研究报告
服务器·ios·unix·pve·freebsd