37

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.

16
  • 9
    Nice find – filed a bug: bugs.swift.org/browse/SR-7315
    – Hamish
    Mar 30, 2018 at 23:27
  • 2
    @Hamish - That seems premature to me. Turn on "whole module" compilation.
    – Rob
    Mar 30, 2018 at 23:31
  • 2
    @Rob That didn't make a difference for me (edit: oh, looks it depends on the order of the files in "compile sources"). Though it shouldn't make a difference anyway – the compiler shouldn't give you different behaviour under whole module compilation (it's purpose is to allow for more aggressive optimisations).
    – Hamish
    Mar 30, 2018 at 23:35
  • 1
    I'm with @Hamish; looks like a compiler bug. Thanks for opening it.
    – Rob Napier
    Mar 31, 2018 at 13:23
  • 1
    @Andrew yeah, reordering does help with Whole Module compilation, that's mentioned in bug report. Please vote for the issue on bugs.swift.org/browse/SR-7315
    – EBDOKUM
    Apr 2, 2018 at 15:41

4 Answers 4

24
+50

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.

4
  • 1
    The steps in that article is outdated as of Xcode 9.3, that option got moved and renamed. Its now under "Compilation Mode" named "Whole Module". Also won't give you bounty as your answer is literally the exact steps I said didn't work in the bounty caption :P Apr 5, 2018 at 5:36
  • 1
    @OscarApeland Thank you for the information, I edited it in. I was not aware of your bounty when I wrote this answer and I tried to organize the solutions that I found working. Also, bringing files with errors to the front was not mentioned in the comments. I am fully against plagiarizing and copy-pasting answers from comments, I always add some additional information (and if not, I mark my answer as community wiki). Apr 5, 2018 at 8:21
  • 1
    No worries, great answer, it obviously worked for some people:) The JIRA bug just got assigned to some dude at Apple, so hopefully we will get some real answers soon. Apr 6, 2018 at 5:54
  • Unfortunately this doesn't work when I want to create Swift object in Objective C. I tried reordering Swift file to the top and then ObjC class to the top and neither of that works.
    – Josip B.
    Apr 20, 2018 at 14:35
21

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") 
    }
}
4
  • Thank you, this worked. Interestingly some of my Codable classes were fixed by project settings change suggested above, while others required this fix as well. Apr 9, 2018 at 11:58
  • Yeah, it seems to require a bit of both. Hopefully they will sort this one out quickly Apr 9, 2018 at 12:25
  • It works. If you mark the init as private you don't have to worry about availability
    – manueGE
    Aug 31, 2018 at 13:46
  • @manueGE That's even smarter. I forgot initialisers had access control. Updated answer. Aug 31, 2018 at 17:58
0

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

1
  • That's because structs get another init synthesized automatically Jun 8, 2018 at 6:49
-3

try to give your variable an initial value like this (change your code to this)

class CodableOne: Codable{

    var some = ""

}

class CodableTwo: Codable{

    var some = ""

}
1
  • 2
    No, you can not do this. This will override the decoded value. Apr 10, 2018 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.