Using Codable to instantiate a single class from 2 different JSON files without using optionals

1824 views ios
1

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?

answered question

Short answer, you can’t - unless you use a default value for coordinate. It looks to me like these are 2 separate objects - one that can be shown on a map, and one that can’t. What do the 2 JSON objects represent?

The second JSON object represents non-changing information about a physical location with coordinates. The first JSON represents attributes that do change, but which belong to the physical location decoded in the second JSON object.

I think you need to re-think your data structures. It seems like JSON2 object should contain a collection of JSON1 objects

1 Answer

10

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

posted this

Have an answer?

JD

Please login first before posting an answer.