使用 Codable 从 2 个不同的 JSON 文件中实例化单个 class 而不使用选项
Using Codable to instantiate a single class from 2 different JSON files without using optionals
我正在使用提供 2 JSON URLS 的 API。每个 URL 包含一个嵌套容器,其中包含属于相同 class 和对象的不同属性。
JSON URL 1
{
"last_updated": 1535936629,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"a1": "a1value",
"a2": "a2value",
},
// ,,,
]
}
}
JSON URL 2
{
"last_updated": 1536639996,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"lat": "12.345",
"lon": "67.890",
},
// ,,,
]
}
}
我想使用这些 JSON URLS 使用嵌套 dataList
列表中的项目创建单个 Codable CustomClass 对象,所以我创建了一个 Feed
处理这 2 个 JSON 文件的结构。
Feed.swift
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)
}
}
CustomClass.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?)
}
}
我收到以下错误
Non-'@objc' property 'coordinate' does not satisfy requirement of '@objc' protocol 'MKAnnotation'
我认为我 运行 遇到的问题是 coordinate
必须是非可选的。但是,lat
和 lon
变量在解码 URL1 时将始终为 nil,因为它们不存在于那个 JSON 文件中。如何在不设置选项的情况下从这 2 JSON URL 解码,以便我可以设置 coordinate
变量?
MKAnnotation
需要一个只读变量 coordinate
所以使用计算 属性:
var coordinate : CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: lat ?? 0.0, longitude: lon ?? 0.0)
}
另请参阅我对您 的回答,建议使用多个 structs/classes 和泛型,如果您想避免可选
,这是唯一的方法
我正在使用提供 2 JSON URLS 的 API。每个 URL 包含一个嵌套容器,其中包含属于相同 class 和对象的不同属性。
JSON URL 1
{
"last_updated": 1535936629,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"a1": "a1value",
"a2": "a2value",
},
// ,,,
]
}
}
JSON URL 2
{
"last_updated": 1536639996,
"xyz": 5,
"data": {
"dataList": [
{
"id": "42",
"lat": "12.345",
"lon": "67.890",
},
// ,,,
]
}
}
我想使用这些 JSON URLS 使用嵌套 dataList
列表中的项目创建单个 Codable CustomClass 对象,所以我创建了一个 Feed
处理这 2 个 JSON 文件的结构。
Feed.swift
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)
}
}
CustomClass.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?)
}
}
我收到以下错误
Non-'@objc' property 'coordinate' does not satisfy requirement of '@objc' protocol 'MKAnnotation'
我认为我 运行 遇到的问题是 coordinate
必须是非可选的。但是,lat
和 lon
变量在解码 URL1 时将始终为 nil,因为它们不存在于那个 JSON 文件中。如何在不设置选项的情况下从这 2 JSON URL 解码,以便我可以设置 coordinate
变量?
MKAnnotation
需要一个只读变量 coordinate
所以使用计算 属性:
var coordinate : CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: lat ?? 0.0, longitude: lon ?? 0.0)
}
另请参阅我对您