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

相关推荐
用户091 天前
SwiftUI Charts 函数绘图完全指南
ios·swiftui·swift
YungFan1 天前
iOS26适配指南之UIColor
ios·swift
权咚2 天前
阿权的开发经验小集
git·ios·xcode
用户092 天前
TipKit与CloudKit同步完全指南
ios·swift
法的空间2 天前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
2501_915918412 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张2 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
Magnetic_h2 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张3 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
前端小超超3 天前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa