0

I inherited Codable class like below.

class Vehicle: Codable {
    let id: Int
    let name: String
    
    init(id: Int, name: String) {
        self.id = id
        self.name = name
    }
}

class Car: Vehicle {
    let type: String
    
    init(id: Int, name: String, type: String) {
        self.type = type
        super.init(id: id, name: name)
    }
}

And it shows error

'required' initializer 'init(from:)' must be provided by subclass of 'Vehicle'

What is a proper way to inherit codable class?

Bigair
  • 1,452
  • 3
  • 15
  • 42

1 Answers1

1

If you allow Xcode to fix the issue, it adds this method to the subclass:

required init(from decoder: Decoder) throws {
    fatalError("init(from:) has not been implemented")
}
McNail
  • 100
  • 8
  • So I leave this fatal error like we do when creating custom init for UIView subclasses and everything works fine? – Bigair Mar 09 '21 at 01:18
  • 1
    Have a look here for how to implement it: [link](https://stackoverflow.com/a/44605696/14506724) – McNail Mar 09 '21 at 01:25