Codable 'has no initializers' in Xcode 9.3 (Swift 4.1) Codable 'has no initializers' in Xcode 9.3 (Swift 4.1) xcode xcode

Codable 'has no initializers' in Xcode 9.3 (Swift 4.1)


As mentioned in the comments, I had to do two things:

  1. changing Compilation Mode to Whole Module inside Project settings/Build Settings:

    Compilation Mode set to Whole Module

  2. reordering the files under Project settings/Build Phases/Compile Sources. Specifically, I brought the files that had an error to the front of the list.

    Protip: if you search for the name of the file and there is more than one result, dragging the file to the top in that smaller list will still bring it to the front.


This is a bug in the Swift 4.1 compiler. To work around it, either do the steps outlined in the4kman's answer, or simply change let to var in your declaration, as such:

class C1 : Decodable {   let str: String   // error: Class 'C1' has no initializers - if class C's `c1` is a let constant. }class C : Decodable {  var c1: C1 // << Change to `var`, compilation succeeds.}

Workaround courtesy of Apples Swift engineers.

If neither this nor the4kmans answer helps, you can add another init to the models who won't compile. If your classes have tons of variables, just crash the init to satisy the compiler. The Codable initializer will still be synthesized.

class C1: Decodable {    let str: String    @available(*, deprecated, message: "Do not use.")    private init() {        fatalError("Swift 4.1")     }}


i had this issue even though all my classes were in the same file, using structs for the deeper ones seems to work though