假设我们有这个枚举:
enum NumberEnumSpecial: Int32 {
case two = 2, three = 3
}
我想用 Int32 初始化它,所以我使用这个:
let myEnum = NumberEnumSpecial.init(rawValue: 2)
这适用于 playground 项目,但不适用于我的常规 App 项目。对于完全相同的代码,我收到此错误:
Ambiguous reference to member 'init(from:)'
/Users/sjoerd/GitHub/flitsmeister-ios/app/Flitsmeister7/Model/Melding/DangerZone.swift:91:22: error: ambiguous reference to member 'init(from:)'
let myEnum = NumberEnumSpecial.init(rawValue: 2)
^~~~~~~~~~~~~~~~~
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Swift.RawRepresentable:2:24: note: found this candidate
public convenience init(from decoder: Decoder) throws
^
Build failed 13/10/2017, 09:32
点击候选人没有任何效果。
如果你问我,代码中某处似乎有一个 Enum,它的 init(from) 实现导致我的 Enum 出现此错误。但是搜索此文本没有任何结果。
这是什么错误以及如何找出导致此错误的原因?
使用 Swift 3.2 和 XCode9.0
目前的解决方法:
enum NumberEnumSpecial: Int32 {
case two = 2, three = 3
init?(withSpecialNumber number : Int32) {
self.init(rawValue: number)
}
}
最佳答案
我在使用 Xcode 9.2 beta (9C32c) 时遇到了同样的问题,如果它是一个错误,它在这个版本中仍然没有修复。我找到了一种解决方法,可以在不覆盖 init
的情况下使错误消失。
我改变了这个:
NumberEnumSpecial.init(rawValue: 2)
为此:
NumberEnumSpecial(rawValue: 2)
关于swift - 对 Enum 的成员 'init(from:)' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46724487/