我选择了手动解决方案:
struct Geometry:Codable {
let type:String
let latitude: Double
let longitude: Double
enum CodingKeys: String, CodingKey {
case type, coordinates
}
enum CoordinatesKeys: String, CodingKey {
case latitude, longitude
}
init (from decoder :Decoder ) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(String.self, forKey: .type)
let coords:[Double] = try container.decode([Double].self, forKey: .coordinates)
if coords.count != 2 { throw DecodingError.dataCorruptedError(forKey: CodingKeys.coordinates, in: container, debugDescription:"Invalid Coordinates") }
latitude = coords[1]
longitude = coords[0]
}
func encode(to encoder: Encoder) throws {
}
}