swift 4.2 在类型 '?' 中找不到枚举大小写 switch 语句中要求冗余解包隐式解包的可选



我有以下枚举:

enum ExampleEnum {
case one
case two
case three
case four  
}

以及以下属性定义:

var exampleProperty: ExampleEnum!

在 swift 4.2 之前,我使用以下 switch 语句:

switch self.exampleProperty {
case .one:
print("case one")
case .two:
print("case two")
case .three:
print("case three")
case .four:
print("case four")
default:
break
}

自从切换到 swift 4.2 以来,这个开关语句给了我一个错误:

Enum case 'one' not found in type 'ExampleEnum?'

我觉得这很奇怪,因为我已经用感叹号清楚地定义了类型,以隐式地解开可选包。 然而,它似乎没有这样做。 为了使错误消失,我需要执行以下操作的切换:

switch self.exampleProperty! {
case .one:
print("case one")
case .two:
print("case two")
case .three:
print("case three")
case .four:
print("case four")
}

我上面所做的是再次解开 exampleProperty 变量的包装,即使定义是隐式解包的,并且还从开关中删除默认值。

只是想知道为什么 swift 4.2 会发生这种变化? 是 switch 语句中的更改,还是为什么需要再次解包。 好像多余了?

对我来说它有效

if let myEnumCase = self.exampleProperty {
switch myEnumCase {
case .one:
print("case one")
case .two:
print("case two")
case .three:
print("case three")
case .four:
print("case four")
}
}

但我仍然感到困惑,如果我们有强制包裹,那么为什么我们需要这个
任何建议都将不胜感激

最新更新