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

相关推荐
牛马1111 小时前
iOS :Codable协议,字典,数组的详细分析和比较
ios
TheNextByte16 小时前
如何将 iPhone 备份到外置硬盘?
ios·iphone
佛系打工仔7 小时前
绘制K线第三章:拖拽功能实现
android·前端·ios
游戏开发爱好者810 小时前
2025年iOS应用上架App Store全指南,开发者必看
android·ios·小程序·https·uni-app·iphone·webview
HH思️️无邪11 小时前
Flutter 项目 -从 0 到 1 实现 iOS WatchApp
flutter·ios
TheNextByte112 小时前
如何将 iPhone 上的联系人备份到 iCloud?
ios·iphone·icloud
牛马11112 小时前
iOS swift 自定义View
ios·cocoa·swift
2501_9151063213 小时前
HBuilderX 项目上架 iOS app上架 App Store 的关键流程
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063213 小时前
iOS 文件管理,在不越狱的前提下管理 iPhone / iPad 文件
android·ios·小程序·uni-app·iphone·webview·ipad
牛马11113 小时前
ios swift处理json数据
ios·json·swift