阅读 79

Swift模型解析

相由心生

题记

此篇是记录swift使用codable模型解析, 遇到的问题.

开始表演

json 结构 {     "foods": [         {             "id": "100261764",             "nm": "轻芝士蛋糕",             "pic": "http://test.com/2e/aaa.jpg"         },         {                         "id": "100445983",             "nm": "《Tinrry+》戚风蛋糕(6寸配方)(基础、超详细教程)"         }     ],     "id": "40072",     "nm": "下午茶" } 复制代码

模型结构体 struct TopicDto: Codable, Identifiable {     let id: String     let nm: String     let foods: [FoodDto] } struct FoodDto: Codable, Identifiable {     let id: String     let pic: String     let nm: String } 复制代码

解析 let jsonData = “”.data(using: .utf8) let decoder = JSONDecoder() let topic = try? decoder.decode(TopicDto.self, from: jsonData) if topic == nil {     print("模型解析失败") } 复制代码

错误的路

由于解析一个[TopicDto]数组结构, 没有关注到topic解析失败, 误以为Topic节点的foods节点均为空,然后走上了

struct TopicDto: Codable, Identifiable {     let id: String     let nm: String     let foods: [FoodDto]     enum CodginKeys: String, CodingKey { case id, nm, foodarr }     enum FoodKeys: String, CodginKey { case foods }     init(from decoder: Decoder) throws {         let values = try decoder.container(keyedBy: CodingKeys.self)         id = try values.decode(String.self, forKey: .id)         nm = try values.decode(String.self, forKey: .nm)         let foodDicts = try values.nestedContainer(keyedBy: FoodKeys.self, forKey: .foodarr)     } } 复制代码

这样一错再错.

正确的发现问题

发现解析出来的topic为nil, 我方才若有所悟, 开始检查我的模型结构体和json数据的对比(此处是简化的结构体), 猜测是不是又是“有与无”的哲学问题导致的.

当我试着诸事掉 FoodDto 的 pic 成员时, 能正确解析出topic, 同事topic模型下的foods有两个food

struct FoodDto: Codable, Identifiable {     let id: String     let pic: String?     let nm: String } 复制代码

这样修改完FoodDto, 问题解决.


作者:littleplayer
链接:https://juejin.cn/post/7026999539890388999

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐