Swift 解码 JSON 对 GMSCoordinateBounds 数组的响应
Swift Decode JSON Response to GMSCoordinateBounds Array
在我的应用程序中,我有以下 JSON 响应和结构,它们代表活动工作区域
JSON :
{
"status_code": 1000,
"data": [
{
"name": "BWME23DW",
"north_east_lat": 33.34534,
"north_east_lng": 44.56467
"south_west_lat": 34.89434,
"south_west_lng": 44.54567
},
],
"message": null
}
结构:
import Foundation
import CoreLocation
import GoogleMaps
struct ActiveBounds : Codable {
var status_code : Int!
var data : [LatLngBounds]!
var message : String!
}
struct LatLngBounds : Codable{
var name : String!
var north_east_lat : CLLocationDegrees!
var north_east_lng : CLLocationDegrees!
var south_west_lat : CLLocationDegrees!
var south_west_lng : CLLocationDegrees!
enum CodingKeys: String, CodingKey {
case name
case north_east_lat
case north_east_lng
case south_west_lat
case south_west_lng
}
}
解码响应后,我需要检查用户当前位置是否在活动范围内,使用 GMSCoordinateBounds.contains(latLong)
非常容易
所以我如何直接在我的 ActiveBounds 结构中解码和初始化它 return 数据 属性 作为 GMSCoordinateBounds 数组而不是 LatLngBounds 结构
这就是我想要完成的
import Foundation
import CoreLocation
import GoogleMaps
struct ActiveBounds : Codable {
var status_code : Int!
var data : [GMSCoordinateBounds]!
var message : String!
}
无需在属性末尾写 !
...
更简单的方法是在 ActiveBounds
上创建惰性变量或计算 属性,它将 [LatLngBounds]
转换为 [GMSCoordinateBounds]
。
struct ActiveBounds : Codable {
var status_code : Int
var data: [LatLngBounds]
var message: String
lazy var coordinatesBounds: [GMSCoordinateBounds] = {
data.map { GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: [=10=].north_east_lat,
longitude: [=10=].north_east_lng),
coordinate: CLLocationCoordinate2D(latitude: [=10=].south_west_lat,
longitude: [=10=].south_west_lng) }
}()
}
这样,您就不会“更改”需要自定义 init(from decoder:)
的 JSON 模型。
在我的应用程序中,我有以下 JSON 响应和结构,它们代表活动工作区域
JSON :
{
"status_code": 1000,
"data": [
{
"name": "BWME23DW",
"north_east_lat": 33.34534,
"north_east_lng": 44.56467
"south_west_lat": 34.89434,
"south_west_lng": 44.54567
},
],
"message": null
}
结构:
import Foundation
import CoreLocation
import GoogleMaps
struct ActiveBounds : Codable {
var status_code : Int!
var data : [LatLngBounds]!
var message : String!
}
struct LatLngBounds : Codable{
var name : String!
var north_east_lat : CLLocationDegrees!
var north_east_lng : CLLocationDegrees!
var south_west_lat : CLLocationDegrees!
var south_west_lng : CLLocationDegrees!
enum CodingKeys: String, CodingKey {
case name
case north_east_lat
case north_east_lng
case south_west_lat
case south_west_lng
}
}
解码响应后,我需要检查用户当前位置是否在活动范围内,使用 GMSCoordinateBounds.contains(latLong)
非常容易
所以我如何直接在我的 ActiveBounds 结构中解码和初始化它 return 数据 属性 作为 GMSCoordinateBounds 数组而不是 LatLngBounds 结构
这就是我想要完成的
import Foundation
import CoreLocation
import GoogleMaps
struct ActiveBounds : Codable {
var status_code : Int!
var data : [GMSCoordinateBounds]!
var message : String!
}
无需在属性末尾写 !
...
更简单的方法是在 ActiveBounds
上创建惰性变量或计算 属性,它将 [LatLngBounds]
转换为 [GMSCoordinateBounds]
。
struct ActiveBounds : Codable {
var status_code : Int
var data: [LatLngBounds]
var message: String
lazy var coordinatesBounds: [GMSCoordinateBounds] = {
data.map { GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: [=10=].north_east_lat,
longitude: [=10=].north_east_lng),
coordinate: CLLocationCoordinate2D(latitude: [=10=].south_west_lat,
longitude: [=10=].south_west_lng) }
}()
}
这样,您就不会“更改”需要自定义 init(from decoder:)
的 JSON 模型。