升级到Xcode 11 / iOS 13之后,先前有效的单元测试失败,因为JSONDecoder
抱怨期望的密钥丢失。
我要解析的JSON:
{
"documentation_url": "http://www.discogs.com/developers/",
"statistics": {
"labels": 1420380,
"releases": 11630155,
"artists": 6465084
},
"hello": "Welcome to the Discogs API.",
"api_version": "v2"
}
我要解码的类型:
public struct DiscogsInfo: Codable {
public var apiVersion: String?
public var documentationUrl: URL
public var hello: String
public var statistics: Stats
public struct Stats: Codable {
public var labels: Int
public var artists: Int
public var releases: Int
}
}
错误:
(lldb) po error
▿ JSONErr
▿ parseFailed : 1 element
▿ error : DecodingError
▿ keyNotFound : 2 elements
- .0 : CodingKeys(stringValue: "documentationUrl", intValue: nil)
▿ .1 : Context
- codingPath : 0 elements
- debugDescription : "No value associated with key CodingKeys(stringValue: \"documentationUrl\", intValue: nil) (\"documentationUrl\")."
- underlyingError : nil
还有解码器:
(lldb) po jsonDecoder.keyDecodingStrategy
Foundation.JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase
我唯一能想到的就是iOS 13以某种方式忽略了keyDecodingStrategy
。