前言
Swift 4是蘋果計(jì)劃于2017年秋季推出的最新版本,其主要重點(diǎn)是提供與Swift 3代碼的源兼容性,并努力實(shí)現(xiàn)ABI穩(wěn)定性。從Swift4開始提供的Decodable解析JSON確實(shí)很方便,但遇到一個(gè)小問題,記錄一下。
當(dāng)JSON中某個(gè)key的值為{}或者空字符串”“,而該值需要解析的不是基本類型時(shí),即使標(biāo)記為 Optional,依然會(huì)導(dǎo)致整個(gè)解析失敗:
//: Playgroundimport Foundation//Book.swift/217056.html">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已經(jīng)是 Optional,為何整個(gè)book都解析失敗?print(book2)
原因:
因?yàn)閏over是 Optional,所以會(huì)調(diào)用 decodeIfPresent 來(lái)解析,而在cover節(jié)點(diǎn)中沒有找到url,thumb_url這兩個(gè)key,導(dǎo)致默認(rèn)解析失敗,直接拋出了錯(cuò)誤。
解決:
重新實(shí)現(xiàn) decodeIfPresent,在解析失敗時(shí)返回nil而不是拋出錯(cuò)誤導(dǎo)致整個(gè)解析失?。?/p>
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
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VEVB武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選