虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Swift编程 > Swift利用Decodable解析JSON的一个小问题详解

Swift利用Decodable解析JSON的一个小问题详解
类别:Swift编程   作者:码皇   来源:互联网   点击:

这篇文章主要给大家介绍了关于Swift利用Decodable解析JSON的一个小问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

Swift 4是苹果计划于2017年秋季推出的最新版本,其主要重点是提供与Swift 3代码的源兼容性,并努力实现ABI稳定性。从Swift4开始提供的Decodable解析JSON确实很方便,但遇到一个小问题,记录一下。

当JSON中某个key的值为{}或者空字符串”“,而该值需要解析的不是基本类型时,即使标记为 Optional,依然会导致整个解析失败:

    //: Playgroundimport Foundation//Book.swiftstruct Book: Codable {
    var id: Int var name: String var cover: BookCover? enum CodingKeys: String, CodingKey {
    case id case name case cover }
    struct BookCover: Codable {
    var url: String var thumbURL: String enum CodingKeys: String, CodingKey {
    case url case thumbURL = "thumb_url" }
    }
    }
    //JSONlet bookJSON1 ="""{
    "id": 1, "name": "fake name 1", "cover": {
    "url": "a.png", "thumb_url": "b.png" }
    }
    """let bookJSON2 ="""{
    "id": 2, "name": "fake name 2", "cover": {
    }
    }
    """//解析let decoder = JSONDecoder()decoder.dateDecodingStrategy = .iso8601let book1 = try? decoder.decode(Book.self, from: bookJSON1.data(using: .utf8)!)// 解析正常print(book1)let book2 = try? decoder.decode(Book.self, from: bookJSON2.data(using: .utf8)!)// 输出 nil,cover已经是 Optional,为何整个book都解析失败?print(book2)

原因:

因为cover是 Optional,所以会调用 decodeIfPresent 来解析,而在cover节点中没有找到url,thumb_url这两个key,导致默认解析失败,直接抛出了错误。

解决:

重新实现 decodeIfPresent,在解析失败时返回nil而不是抛出错误导致整个解析失败:

    extension KeyedDecodingContainer {
    public func decodeIfPresent<T>(_ type: T.Type, forKey key: K) throws -> T? where T : Decodable {
    return try? decode(type, forKey: key) }
    }

参考: http://davelyon.net/2017/08/16/jsondecoder-in-the-real-world

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:

  • Swift中字典与JSON转换的方法
  • iOS Swift读取本地json文件报错的解决方法
  • Swift Json实例详细解析
相关热词搜索: swift json解析 swift json swift decodab