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后程序员张6 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
00后程序员张11 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
TheNextByte111 小时前
如何从锁定的 iPhone 中恢复照片?
ios·iphone
Maynor99613 小时前
Clawdbot手机访问完整教程:像用App一样方便
ios·智能手机·iphone
游戏开发爱好者815 小时前
在 Windows、Linux 与 CI 环境下命令行上传 IPA 到 App Store
linux·windows·ios·ci/cd·小程序·uni-app·iphone
符哥200815 小时前
对比ArkTsUI和Flutter和 SwiftUI 和Jetpack Compose四个框架语法及使用场景。
flutter·ios·swiftui
TheNextByte115 小时前
如何在有或没有备份的 iPhone 上检索已删除的短信
ios·iphone
TheNextByte117 小时前
如何在恢复模式下从 iPhone 恢复照片?
ios·cocoa·iphone
TheNextByte117 小时前
【已修复】由于软件版本过旧,无法将备份恢复到此 iPhone
ios·cocoa·iphone
2501_916007471 天前
跨平台 App 安全,Flutter、RN、Unity、H5 混合应用加固
android·ios·小程序·https·uni-app·iphone·webview