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

相关推荐
kevinli3 天前
available没你想象中的可靠
ios·编译原理
CodeJourney_J4 天前
如何通过SMB协议实现手机共享电脑文件
windows·ios·smb
ipad协议开发4 天前
企业微信iPad协议接口深度解析:技术原理、应用场景与开发实战
ios·企业微信·ipad
千里马-horse5 天前
Building a Simple Engine -- Mobile Development -- Platform considerations
android·ios·rendering·vulkan
systeminof5 天前
全新MacBook发布:苹果公司将 iPhone 芯片带入笔记本时代
ios·iphone
pop_xiaoli5 天前
effective-Objective-C 第四章阅读笔记
笔记·ios·objective-c·cocoa·xcode
前端不太难6 天前
Flutter 适合什么团队?RN / iOS 各自的边界在哪?
flutter·ios
带娃的IT创业者6 天前
解密OpenClaw系列10-OpenClaw系统要求
人工智能·macos·ios·objective-c·ai智能体·智能体开发·openclaw