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_915106321 小时前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
一点晖光4 小时前
ios底部按钮被挡住
前端·ios·微信小程序
chinesegf14 小时前
iTunes Lookup API 规则具体(查包名)
ios
C+++Python21 小时前
iOS 长截图
ios
货拉拉技术1 天前
iOS疑难Crash-_dispatch_barrier_waiter_redirect_or_wake 崩溃治理
ios
chinesegf1 天前
iOS 内购收据验证的基础实现
ios
TheNextByte11 天前
如何在没有 Wi-Fi 的情况下备份 iPhone
ios·iphone
2501_915106321 天前
iOS开发中CPU功耗监控的实现与工具使用
android·macos·ios·小程序·uni-app·cocoa·iphone
chinesegf1 天前
如何在沙盒环境中进行内购测试
笔记·ios
TheNextByte11 天前
如何使用数据线或无线方式将照片从Mac传输到 iPhone?
macos·ios·iphone