0

I'm working on a project (Swift 4) and I want to save an array of custom Objects which are displayed on a table view. To do that, I have to adopt the Codable protocol. But I have to adapt my subclass too, and frankly I do not know how. Still a beginner and trying to learn that stuff.

Here some example code, which should represent my problem.

//The first class of Objects I want to save

class Vehicle: Codable {

   let wheels: Int
   let color: String

   init(wheels: Int, color: String) {
      self.wheels = wheels
      self.color = color
   }

}


//The second class of Objects I want to save

class Car: Vehicle {
    let manufacturer: String

    init(wheels: Int, color: String, manufacturer: String) {
         self.manufacturer = manufacturer
         super.init(wheels: wheels, color: color)
    }

    //this method is needed when I implement the Codable protocol to the super class
    required init(from decoder: Decoder) throws {
         //What should I do here?
    }
}

I just don't know how to properly adapt the Subclass to the Codable protocol in order for it to work.

Thomas
  • 75
  • 8
  • It would be helpful for you to include the actual error you're getting. I see a couple of other issues that aren't related to what you say you're seeing. In your Vehicle init, you're decoding `colour` but encoding `color`. Also, in your Car class, you're doing a super.init using a bunch of fields not in your parent class like `verfuegbarkeit` and `kursnummer`. – Chris Allwein Oct 13 '17 at 17:10
  • Sorry, those are typos and snippets of my actual code I copied to make an easier example, which has less variables and other code. If I try to implement the required init function (by decoding the manufacturer variable and then calling super.init(coder: aDecoder)) i get the following error: Property 'self.manufacturer' not initialized at super.init call – Thomas Oct 13 '17 at 17:13
  • If `decodeObject` returns nil for either of the fields, they don't get initialized. Can you change to Swift 4's `Codable` and its automagically generated methods, or do you have to support Swift 3? – Kevin Oct 13 '17 at 17:20
  • If I change it to Codable and delete the required init and encode method, I get the following error in the subclass: 'required' initializer 'init(from:)' must be provided by subclass of 'Vehicles'. – Thomas Oct 13 '17 at 17:29
  • `decodeInteger(forKey:)` method returns an Integer (not optional) so you can't use `if let` – Leo Dabus Oct 13 '17 at 17:31
  • color it is not optional so you need to provide a default value in case your `decodeObject(forKey:"color") as? String` fails. You can use the nil coalescing operator for that `decodeObject(forKey:"color") as? String ?? "default value or empty string"` – Leo Dabus Oct 13 '17 at 17:34
  • And change your properties declaration to constants. `let wheels: Int` and `let color: String` – Leo Dabus Oct 13 '17 at 17:35
  • I deleted that code already, because I changed the protocol to Codable. The problem I still have is how to adapt my Subclass to it. – Thomas Oct 13 '17 at 17:36
  • And you need to make your subclass car also NSCoding compliant – Leo Dabus Oct 13 '17 at 17:36
  • Make them all Codable and use a struct instead of a class – Leo Dabus Oct 13 '17 at 17:37
  • edited my question – Thomas Oct 13 '17 at 17:44

0 Answers0