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

相关推荐
Anhty12 小时前
2026最新免费手机音频处理工具 !!
android·功能测试·ios·智能手机·音视频
2501_9159090614 小时前
iOS应用从开发到上架App Store的完整发布流程与步骤指南
android·ios·小程序·https·uni-app·webview
初雪云15 小时前
UniApp 云打包拿到 IPA 后,真正的发布工作才刚开始
ios·自动化·编辑器·产品经理·iphone
Lancker17 小时前
没有 Mac 也能上架 iOS:GitHub Actions 全自动构建 + 签名 + TestFlight 送审实录
macos·ios·github
茶底世界之下17 小时前
晚霞要多暖?用 Awaking 只改色温做一次取舍
ios
酒神dnspup19 小时前
多地区网站可用性监控怎么做?DNSPup 从基线到告警的完整实战
运维·ios·云计算·iphone·dns·网络监控
福兮说1 天前
浏览器里把 iPhone 的 HEIC 转成 JPG:Chrome 解不开、转完大了七成、拍摄时间全丢,六个坑实测
前端·javascript·图像处理·chrome·ios·iphone·heic
茶底世界之下2 天前
时间线效果层,为什么不能只复用一个处理器?
ios·swift
ZZH_AI项目交付2 天前
实时字幕负责快,本地录音负责不丢:一个 iOS 录音功能为什么需要两条链路
ios·ai编程
Cc.Y2 天前
Swift深度链接的极简之道
ios·cocoa·swift