对Enum的成员'init(from :)'的模糊引用

时间:2017-10-13 07:17:49

标签: swift enums

假设我们有这个枚举:

enum NumberEnumSpecial: Int32 {
    case two = 2, three = 3
}

我想用Int32初始化它,所以我用它:

    let myEnum = NumberEnumSpecial.init(rawValue: 2)

这适用于游乐场项目,但不适用于我的常规App项目。我收到此错误的完全相同的代码:

Ambiguous reference to member 'init(from:)'

enter image description here

/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)实现在我的枚举上导致这个错误。但是搜索这个文本没有给我带来任何结果。

这是什么错误以及如何找出造成这种错误的原因?

使用Swift 3.2和XCode9.0

目前解决方法:

enum NumberEnumSpecial: Int32 {
    case two = 2, three = 3

    init?(withSpecialNumber number : Int32) {
          self.init(rawValue: number)
    }
}

1 个答案:

答案 0 :(得分:27)

我在使用Xcode 9.2 beta(9C32c)时遇到了同样的问题,如果它是一个bug,它仍然没有在这个版本中修复。我找到了一种解决方法,可以在不覆盖init的情况下使错误消失。

我改变了这个:

 NumberEnumSpecial.init(rawValue: 2)

到此:

 NumberEnumSpecial(rawValue: 2)