GMSPath
有一个encodedPath
属性(它是一个字符串),它也可以用一个编码路径初始化。您只需要将您的编码GMSPath
为它的编码路径表示。
符合显式EstimateResponse
实现Codable
:
class EstimateResponse : Codable {
var path: GMSPath?
var destination: String?
var distance: String?
enum CodingKeys: CodingKey {
case path, destination, distance
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let encodedPath = try container.decode(String.self, forKey: .path)
path = GMSPath(fromEncodedPath: encodedPath)
destination = try container.decode(String.self, forKey: .destination)
distance = try container.decode(String.self, forKey: .distance)
}
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)
try container.encode(path?.encodedPath(), forKey: .path)
try container.encode(destination, forKey: .destination)
try container.encode(distance, forKey: .distance)
}
}