【发布时间】:2017-07-11 10:33:01
【问题描述】:
我正在尝试使用 RawRepresentable 的可选参数制作一个通用的可失败初始化程序,基本上是 https://www.natashatherobot.com/swift-failable-enums-with-optionals/
提出了几种方法,其中一种是这样(编辑:在第二个子句中修复了let):
extension RawRepresentable {
init?(rawValue optionalRawValue: RawValue?) {
guard let rawValue = optionalRawValue, let value = Self(rawValue: rawValue) else { return nil }
self = value
}
}
从这里https://gist.github.com/okla/e5dd8fbb4e604dabcdc3
我不知道它是否曾经在 Swift 2 上工作,但我无法在 Swift 3 上编译它。我明白了:
Command failed due to signal: Segmentation fault: 11
有没有办法让它工作?
附:我知道这篇文章及其 cmets 中的其他方法。
编辑:修复损坏的复制/粘贴代码。
【问题讨论】:
-
编译器应该永远不会崩溃,所以这显然是一个错误。然而,它似乎在 Swift 3.1 中得到了修复(Xcode 8.3 beta 可用)。
-
File a bug 当然是关于崩溃的——没有任何代码,无论它多么损坏,都会导致编译器崩溃。
标签: swift initialization protocols