Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

After updating to Xcode 9.3 (which uses Swift 4.1), the following issue was found:

  1. Create an empty project, add a new .swift file to it and create two new classes:

    Created to Codable classes

    class CodableOne: Codable {
    
        let some: String
    
    }
    
    class CodableTwo: Codable {
    
        var some: String
    
    }
    

    Build succeeds

  2. Add a new constant to CodableOne of type CodableTwo:

    Added a new constant to CodableOne

    class CodableOne: Codable {
    
        let some: String
        let another: CodableTwo
    
    }
    
    class CodableTwo: Codable {
    
        var some: String
    
    }
    

    Build succeeds

  3. Now move class CodableTwo to another file (ViewController.swift, for example)

    CodableTwo moved to another file

    Build fails.

Now there's an error, which won't go away. Codable classes should not require initializers (as demonstrated in previous steps).

Any ideas on what could be the problem behind this and how it could be resolved will be much appreciated!


P.S. Issue is not present in Xcode 9.2. Nor cleaning the project/build path, neither re-installing Xcode 9.3 helps.

like image 924
EBDOKUM Avatar asked Mar 30 '18 23:03

EBDOKUM


2 Answers

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.

like image 120
Tamás Sengel Avatar answered Nov 13 '22 00:11

Tamás Sengel


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") 
    }
}
like image 20
Oscar Apeland Avatar answered Nov 13 '22 00:11

Oscar Apeland