gpt4 book ai didi

ios - swift 4.2 枚举案例 ' ' 在类型 '?' 中找不到 switch 语句中的要求以冗余解包隐式解包的可选

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:31 25 4
gpt4 key购买 nike

我有以下枚举:

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 之后,这个 switch 语句给出了错误:

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")
}
}

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

关于ios - swift 4.2 枚举案例 ' ' 在类型 '?' 中找不到 switch 语句中的要求以冗余解包隐式解包的可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com