问题
I'm working with an API that provides 2 JSON URLS. Each URL contains a nested container with different attributes that belong to the same class and object.
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",
},
// ,,,
]
}
}
I want to use these JSON URLS to create a single Codable CustomClass object using the items in the nested dataList
list, so I created a Feed
struct to handle these 2 JSON files.
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?)
}
}
I get the following error
Non-'@objc' property 'coordinate' does not satisfy requirement of '@objc' protocol 'MKAnnotation'
I think the problem I'm running into is that coordinate
needs to be non-optional. However, the lat
and lon
variables will always be nil when decoding URL1, because they're not present in that JSON file. How do I decode from these 2 JSON URLs without setting optionals, so that I can set the coordinate
variable?
回答1:
MKAnnotation
requires a read-only variable coordinate
so use a computed property:
var coordinate : CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: lat ?? 0.0, longitude: lon ?? 0.0)
}
See also my answer to your previous question with the suggestion to use multiple structs/classes and generics which is the only way if you want to avoid optionals
来源:https://stackoverflow.com/questions/52346770/using-codable-to-instantiate-a-single-class-from-2-different-json-files-without