首页 > 解决方案 > 带有参数和 _ 的可编码枚举在编译时失败

问题描述

我注意到我收到了一个非常确定的枚举类型的编译时错误。它可以通过以下方式重现:

enum ThisEnumCompiles {
    case stringArgument(_ string: String)
    case intArgument(_ int: Int)
}

enum ThisEnumAlsoCompiles: Codable {
    case stringArgument(string: String)
    case intArgument(int: Int)
}

enum ThisEnumDoesNot: Codable {
    case stringArgument(_ string: String)
    case intArgument(_ int: Int)
}

在您实际构建之前,XCode 中不会显示最后一个枚举的错误:

<unknown>:0: error: type 'ThisEnumDoesNot' has no member 'stringArgument(string:)'
<unknown>:0: error: type 'ThisEnumDoesNot' has no member 'intArgument(int:)'

我不清楚为什么添加_会使最后一个枚举无法编译,而如果它不符合则很好Codable

标签: iosswiftswift5.5

解决方案



推荐阅读