swift - Xcode 9.3 (Swift 4.1) 中的 Codable 'has no initializers'

标签 swift xcode codable swift4.1 xcode9.3

更新到 Xcode 9.3(使用 Swift 4.1)后,发现以下问题:

  1. 创建一个空项目,向其中添加一个新的 .swift 文件并创建两个新类:

    Created to Codable classes

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

    构建成功

  2. CodableOne 添加一个类型为 CodableTwo 的新常量:

    Added a new constant to CodableOne

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

    构建成功

  3. 现在将类 CodableTwo 移动到另一个文件(例如 ViewController.swift)

    CodableTwo moved to another file

    构建失败

现在有一个错误,不会消失。 Codable 类不应需要初始化程序(如前面的步骤所示)。

任何关于这背后可能是什么问题以及如何解决它的想法将不胜感激!


附言Xcode 9.2 中不存在此问题。既不清理项目/构建路径,重新安装 Xcode 9.3 也无济于事。

最佳答案

作为mentioned in the comments ,我必须做两件事:

  1. Project settings/Build Settings 中将 Compilation Mode 更改为 Whole Module:

    Compilation Mode set to Whole Module

  2. Project settings/Build Phases/Compile Sources 下重新排序文件。具体来说,我将有错误的文件放在列表的前面

    提示:如果您搜索文件名并且有多个结果,将文件拖到那个较小列表的顶部仍会将其带到最前面。

关于swift - Xcode 9.3 (Swift 4.1) 中的 Codable 'has no initializers',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582558/

相关文章:

Xcode 4 项目 : utility to clean up pbxproj file?

ios - 将文件添加到 SVN 源代码管理 > 在 XCode 4 中添加灰色

objective-c - NSGradient 转换为 NSColor

swift - 在 Swift 中合并 Encodable

ios - Swift 中的错误检测终端引用变量作为问题 - 变量不存在

swift - 自动从 Firebase 数据库中删除数据

ios - 为什么 UIView 的 CALayer.speed 为零会阻止 UIAlertController 的按钮工作?

json - 如何在 Swift [45] 可解码协议(protocol)中解码 JSON 字典类型的属性

swift - 使用泛型创建结构数组

ios - 在 xcode ui test 中运行所有测试类