-1

I'm fairly new to dealing with JSON data in Swift and I am trying to subclass some products. I don't mean to code dump, but I want to give you the whole picture. I have three errors that say the same thing: Errors thrown from here are not handled They occur in required init. Thanks in advance. Here's the code:

import UIKit

class Product: Decodable {
    var category: String = ""
    var material: String = ""

    init() {

    }
}

class TelephoneWithCord: Product {

    var sku: Double
    var isNew: Bool

    private enum CodingKeys: String, CodingKey {
        case sku = "sku"
        case isNew = "isNew"
    }

    required init(from decoder: Decoder) {

        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.sku = try container.decode(Double.self, forKey: .sku)
        self.isNew = try container.decode(Bool.self, forKey: .isNew)
    }
}

let json = """

{
    "category" : "home",
    "material" : "plastic",
    "sku" : 264221,
    "isNew" : true
}

""".data(using: .utf8)!

let telephoneWithCord = try! JSONDecoder().decode(TelephoneWithCord.self, from: json)

telephoneWithCord.category
telephoneWithCord.material
telephoneWithCord.sku
telephoneWithCord.isNew

1 Answers1

1

"Errors thrown", could perhaps, be a hint on how to fix this. Add throws to required init. Also, don't forget to call super for your code to be properly initialized or you will get another error. Try these changes ...

required init(from decoder: Decoder) throws {  // add throws to eliminate errors

    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.sku = try container.decode(Double.self, forKey: .sku)
    self.isNew = try container.decode(Bool.self, forKey: .isNew)
    try super.init(from: decoder)  // calling super for proper intialization of code
}

As a side note: If you are not using any decimal points in your sku's, then you should change the type to Int instead of Double.

  • y̶o̶u̶ ̶s̶h̶o̶u̶l̶d̶ ̶c̶h̶a̶n̶g̶e̶ ̶t̶h̶e̶ ̶t̶y̶p̶e̶ ̶t̶o̶ ̶I̶n̶t̶ ̶i̶n̶s̶t̶e̶a̶d̶ ̶o̶f̶ ̶D̶o̶u̶b̶l̶e̶ You shouldn't use a numeric type for this at all. It's not a number, it's a series of digits. – jscs Mar 15 '19 at 18:16
  • @Caswell Are you suggesting to represent it as a `String`? –  Mar 16 '19 at 00:16
  • I think that it is likely represented as an `Int` in the backend, so I would fell more comfortable keeping it that way. I could be wrong. JohnW would know for sure and he should adjust his code accordingly. –  Mar 16 '19 at 00:18
  • Also, if there are some sku's that do have decimals, then it may be wiser to keep all of them as `Doubles`. These are things that JohnW will need to determine. –  Mar 16 '19 at 00:22
  • The backend needs to be fixed too, then. I'm sure everyone will enjoy spending the time spent resolving this one: https://gist.github.com/woolsweater/f7c1ca8f0f1c3ea1152e38acc135c6f0 – jscs Mar 16 '19 at 17:08
  • @Caswell Your saying what you think it shouldn’t be, but not saying what you think it should be. String type or something else? –  Mar 16 '19 at 20:52