我正在使用提供 2 个 JSON URL 的 API。每个 URL 都包含一个嵌套容器,该容器具有属于同一类和对象的不同属性。
JSON 网址 1
{
"last_updated": 1535936629,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"a1": "a1value",
"a2": "a2value",
},
// ,,,
]
}
}
JSON 网址 2
{
"last_updated": 1536639996,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"lat": "12.345",
"lon": "67.890",
},
// ,,,
]
}
}
我想使用这些 JSON URL 来使用嵌套中的项目创建单个 Codable CustomClass 对象dataList
列表,所以我创建了一个Feed
struct 来处理这 2 个 JSON 文件。
快速进给
import Foundation
Struct Feed: Decodable {
var lastUpdated: Int
var xyz: Int
var data: KeyedDecodingContainer<Feed.dataCodingKey>
var dataList: [CustomClass]
enum CodingKeys: String, CodingKey {
case lastUpdated = "last_updated"
case xyz
case data
}
enum dataCodingKey: String, CodingKey {
case dataList
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.lastUpdated = try decoder.decode(Int.self, forKey: .lastUpdated)
self.xyz = try container.decode(Int.self, forKey: .xyz)
self.data = try container.nestedContainer(keyedBy: dataCodingKey.self, forKey: .data)
self.dataList = try data.decode([CustomClass].self, forKey: .dataList)
}
}
自定义类.swift
class CustomClass: NSObject, Decodable, MKAnnotation {
var id: String?
var a1: String?
var a2: String?
var lat: Double?
var lon: Double?
var coordinate: CLLocationCoordinate2D?
enum CodingKeys: String, CodingKey {
case id
case a1
case a2
case lat
case lon
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.id = try values.decode(String.self, forKey: .id)
self.a1 = try values.decodeIfPresent(String.self, forKey: .a1)
self.a2 = try values.decodeIfPresent(String.self, forKey: .a2)
self.lat = try values.decodeIfPresent(Double.self, forKey: .lat)
self.lon = try values.decodeIfPresent(Double.self, forKey: .lon)
self.coordinate = CLLocationCoordinate2D(latitude: self.lat?, longitude: self.lon?)
}
}
我收到以下错误
非“@objc”属性“坐标”不满足“@objc”协议“MKAnnotation”的要求
我认为我遇到的问题是coordinate
需要是非可选的。但是,那lat
and lon
解码 URL1 时变量将始终为零,因为它们不存在于该 JSON 文件中。如何在不设置选项的情况下从这 2 个 JSON URL 进行解码,以便我可以设置coordinate
多变的?