4

我想我理解@unknown default 背后的逻辑,我为截图而不是代码道歉,但这是在上下文中查看错误消息的唯一方法。

将项目转换为 Swift 5。它运行,但收到​​此警告,我理解:

在此处输入图像描述

所以我让 Xcode 为我修复它,并得到了这个:

在此处输入图像描述

我更改了存根案例的顺序(这是一项正在进行的工作),这让编译器再次感到高兴:

在此处输入图像描述

我是在做我不应该做的事情,还是这种奇怪的编译器行为?

4

2 回答 2

8

The error in the middle image looks like a bug in swift and it could be solved by adding a semicolon at the end of the return statement.

In general the compiler is expecting @unknown default to be the last case. Check @unknown documentation from apple where they explain why it must be used with the last case in a switch and more on the "unknown patterns" link in the following quote:

@unknown may only be applied to default or a case consisting of the single pattern _. Even in the latter case, @unknown must be used with the last case in a switch. This restriction is discussed further in the "unknown patterns" section under "Future directions".

于 2019-04-09T10:28:31.533 回答
0

在很多情况下,return一行代码后跟一个裸代码会导致 Swift 认为您正在尝试返回该行代码。这种情况不像以前那么令人困惑了,因为现在至少有一个警告可以告诉你(而且这种情况出现的情况比以前少得多):

@IBAction func doDismiss(_ sender: Any) {
    return
    self.presentingViewController?.dismiss(animated:true)
}

该代码看起来合法,但无法编译,结果是一个看似奇怪的编译错误:

Value of optional type 'Void?' must be unwrapped to a value of type ‘Void'

幸运的是,在这种情况下,现在也通过警告(通常)揭示了奇怪的原因:

Expression following 'return' is treated as an argument of the 'return'

解决方案一直是在return. 事实上,对于我们这些从 Swift 1 开始就一直在使用 Swift 的人来说,在裸字符后添加分号return实际上是一种反射动作,尽管现在通常不再需要它。

你的情况基本上是同一个问题。问题是您没有收到解释性警告。

于 2019-04-09T16:34:17.160 回答