GMSPath в swift Codable не соответствует протоколу swift - PullRequest
0 голосов
/ 19 сентября 2019

Я создал модель и использовал кодируемую с ней.В настоящее время я использую GMSPath для получения пути, но при добавлении в класс модели я получаю сообщение об ошибке Type 'EstimateResponse' does not conform to protocol 'Decodable' и Type 'EstimateResponse' does not conform to protocol 'Encodable'

ниже - моя модель

class EstimateResponse: Codable {

    var path: GMSPath? // Set by Google directions API call
    var destination: String?
    var distance: String?
}

любая помощьоценил

1 Ответ

0 голосов
/ 19 сентября 2019

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)
    }
}
...