前言
Swift 4是蘋果計劃于2017年秋季推出的最新版本,其主要重點是提供與Swift 3代碼的源兼容性,并努力實現ABI穩定性。從Swift4開始提供的Decodable解析JSON確實很方便,但遇到一個小問題,記錄一下。
當JSON中某個key的值為{}或者空字符串”“,而該值需要解析的不是基本類型時,即使標記為 Optional,依然會導致整個解析失敗:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//: Playground import Foundation //Book.swift struct 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" } } } //JSON let 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 = .iso8601 let 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而不是拋出錯誤導致整個解析失敗:
1
2
3
4
5
|
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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://antscript.com/post/2018-03-14-iOS-decodable-empty-json/