编码:手动处理一键

时间:2018-10-27 19:10:54

标签: ios json swift codable

我从API获取JSON,并且大多数键都是开箱即用的。

API响应示例:

[
  { "href":"http:\/\/audiomachine.com\/",
    "description":"Audiomachine",
    "extended":"Music for videos",
    "shared":"yes",
    "toread":"no",
    "tags":"creative video music"
  },
  { "href": "https:\/\/www.root.cz\/clanky\/nekricte-na-disky-zvuk-i-ultrazvuk-je-muze-poskodit-nebo-zmast-cidla\/",
    "description":"Nek\u0159i\u010dte na disky: zvuk i\u00a0ultrazvuk je m\u016f\u017ee po\u0161kodit nebo zm\u00e1st \u010didla - Root.cz",
    "extended":"Added by Chrome Pinboard Extension",
    "shared":"yes",
    "toread":"no",
    "tags":"root.cz ril hardware webdev"
  },
  { "href": "https:\/\/www.premiumbeat.com\/blog\/10-apple-motion-tutorials-every-motion-designer-watch\/",
    "description":"10 Apple Motion Tutorials Every Motion Designer Should Watch",
    "extended":"For people used to working with FCPX, sometimes learning Apple Motion can be much easier than learning After Effects. While the two programs are different, there\u2019s certainly a lot of overlap in terms of functionality and creative potential. The following ten Apple Motion tutorials are great examples of just that.\r\n\r\nWhether you are someone new to Apple Motion or a seasoned veteran looking to up your skills, here are ten must watch Apple Motion tutorials.",
    "shared":"yes",
    "toread":"no",
    "tags":"apple apple_motion video creative effects"
  }
]

我将每个项目存储在以下结构中。

struct Link: Codable {
    let href: URL
    let description: String
    var tags: String
}

然后用

解码
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
    guard let data = data, error == nil else { return }

    do {
        let decoder = JSONDecoder()
        let decodedData = try decoder.decode([Link].self, from: data)

从API数据中,我使用 href 说明标签 Tags 有点麻烦,因为我想在代码中将其用作字符串数组,但是在JSON中,它会作为包含所有用空格分隔的标签的String检索。因为这是我的第一个项目,所以我经常学习iOS开发,所以我几乎不知道如何解决这个问题。

我可以通过谷歌搜索计算属性来接近,所以可能是

struct Link: Codable {
    let href: URL
    let description: String
    var tags: String {
        return tagsFromServer.components(separatedBy: " ")
    }
}

,但是此解决方案有两个问题。

  1. 我有一种强烈的感觉,每次代码访问.tags时都会调用此闭包。我宁愿在JSON检索后再执行一次,之后再读一次。
  2. 我完全不知道如何将“我从服务器获得的原始标签” / tagsFromServer传递给闭包。

我也很高兴填补我在术语方面的空白或有关此主题的精美视频教程。

1 个答案:

答案 0 :(得分:1)

我建议编写一个自定义初始化程序,将tags解码为String并将其拆分:

struct Link: Decodable {
    let href: URL
    let description: String
    let tags: [String]

    private enum CodingKeys: String, CodingKey { case href, description, tags }

    init(from decoder: Decoder) throws {
       let container = try decoder.container(keyedBy: CodingKeys.self)
       href = try container(URL.self, forKey: .href)
       description = try container(String, forKey: .description)
       let tagsFromServer = try container(String.self, forKey: .tags)
       tags = tagsFromServer.components(separatedBy: " ")
    }  
}

或者,您可以使用计算属性,但是必须添加CodingKeys

struct Link: Decodable {
    let href: URL
    let description: String
    let tagsFromServer : String

    private enum CodingKeys: String, CodingKey { case href, description, tagsFromServer = "tags" }

    var tags: [String] {
        return tagsFromServer.components(separatedBy: " ")
    }
}