迅捷的4个Firebase GeoPoint解码 [英] swift 4 firebase GeoPoint decode

查看:58
本文介绍了迅捷的4个Firebase GeoPoint解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用标准swift 4从Firebase的JSON响应中编码和解码GeoPoint?

I was wondering if it is possible to encode and decode a GeoPoint from the firebase's JSON response using standard swift 4?

到目前为止,GeoPoint似乎不可编码吗?

It looks like as of now that the GeoPoint is not Codable?

我收到以下错误

No 'decode' candidates produce the expected contextual result type 'GeoPoint'

在我的可编码课程中

final class Data: Codable
{
  var location:GeoPoint = GeoPoint(latitude:0,longitude:0)

  private enum CodingKeys: String, CodingKey
  {
    case geoloc
  }

  init(from decoder: Decoder) throws
  {
    let values = try decoder.container(keyedBy: CodingKeys.self)

    do
    {
      located = try values.decode(Location.self, forKey: .geoloc) //error here
    }
    catch
    {
      print("data has location in server response\n")
    }

  }

  func encode(to encoder: Encoder) throws
  {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(location, forKey: .geoloc)

  }
}

推荐答案

我能够扩展GeoPoint类并使其可编码.这就是我解决的方法.

I was able to extend the GeoPoint class and make it Codable. That is how I solved it.

import UIKit

final class MyGeoPoint: GeoPoint, Codable
{
  override init(latitude: Double, longitude: Double)
  {
    super.init(latitude: latitude, longitude: longitude)
  }

  private enum CodingKeys: String, CodingKey
  {
    case latitude  = "_latitude"
    case longitude = "_longitude"
  }

  init(from decoder: Decoder) throws
  {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    var lat:Double   = 0
    var lon:Double  = 0

    do
    {
      lat = try container.decode(Double.self, forKey: .latitude)
    }
    catch
    {
      print("no latitude for MyGeoPoint")
    }

    do
    {
      lon = try container.decode(Double.self, forKey: .longitude)
    }
    catch
    {
      print("no longitude for MyGeoPoint")
    }


    super.init(latitude: lat, longitude: lon)
  }

  func encode(to encoder: Encoder) throws
  {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(latitude,  forKey: .latitude)
    try container.encode(longitude, forKey: .longitude)
  }

}

现在,我可以使用扩展的MyGeoPoint类(而不是直接使用Google的GeoPoint)来使用原始的Data类来使用Google的JSON响应

Now I can use my original Data class to consume the JSON response from Google, using my extended MyGeoPoint class (instead of Google's GeoPoint directly)

final class Data: Codable
{
  var location:MyGeoPoint = MyGeoPoint(latitude:0,longitude:0)
  private enum CodingKeys: String, CodingKey
  {
    case geoloc
  }
  init(from decoder: Decoder) throws
  {
    let values = try decoder.container(keyedBy: CodingKeys.self)

    do
    {
      //no error here anymore
      location = try values.decode(MyGeoPoint.self, forKey: .geoloc) 
    }
    catch
    {
      print("data has location in server response\n")
    }

  }

  func encode(to encoder: Encoder) throws
  {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(location, forKey: .geoloc)

  }
}

这篇关于迅捷的4个Firebase GeoPoint解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆