如果您发现自己多次需要这个,那么您可以构建自己的通用结构来解码它找到的任何键:
struct Nester<T: Decodable>: Decodable {
let elements: [T]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let key = container.allKeys.first {
elements = try container.decode([T].self, forKey: key)
} else {
// we run into an empty dictionary, let's signal this
throw DecodingError.typeMismatch([String:Any].self, DecodingError.Context(codingPath: [], debugDescription: "Expected to find at least one key"))
}
}
// A coding key that accepts whatever string value it is given
struct CodingKeys: CodingKey {
let stringValue: String
var intValue: Int? { nil }
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) { return nil }
}
}
有了这个,您可以扩展JSONDecoder
以获得更好的呼叫站点:
extension JSONDecoder {
func decode<T: Decodable>(nested: [T].Type, from data: Data) throws -> [T] {
try decode(Nester<T>.self, from: data).elements
}
}
然后只需调用新的重载即可:
let places = try JSONDecoder().decode(nested: [Place].self, from: data)
PS 如果你愿意,你可以在扩展中隐藏复杂的结构,结果是这样的:
extension JSONDecoder {
func decode<T: Decodable>(nested: [T].Type, from data: Data) throws -> [T] {
try decode(Nester<T>.self, from: data).elements
}
private struct Nester<T: Decodable>: Decodable {
let elements: [T]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let key = container.allKeys.first {
elements = try container.decode([T].self, forKey: key)
} else {
throw DecodingError.typeMismatch([String:Any].self, DecodingError.Context(codingPath: [], debugDescription: "Expected to find at least one key"))
}
}
struct CodingKeys: CodingKey {
let stringValue: String
var intValue: Int? { nil }
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) { return nil }
}
}
}
不利的一面是,如果您想扩展除 JSON 之外的其他解码器,您将无法重用该结构。