swift - 对 Enum 的成员 'init(from:)' 的引用不明确

标签 swift enums

假设我们有这个枚举:

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

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

    let myEnum = NumberEnumSpecial.init(rawValue: 2)

这适用于 playground 项目,但不适用于我的常规 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) 实现导致我的 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/

相关文章:

c# - 静态对象为空/未在正确时间初始化

c# - WCF 数据服务是否有任何解决方法来绕过 OData V3 枚举支持的缺失?

SwiftUI:从屏幕上的 CGPoint 转换为 View 中的 CGPoint

ios - UILabel 上的 instrictContentSize 在不同的实例中返回不同的值

c# - '^' 在 c#(枚举)中做什么?

enums - Rust中的静态枚举

java - 在 Eclipse 中使用指定的类加载器加载单例

javascript - 为什么云函数返回 ‘Optional()’ 的值

swift - Xcode、Swift 3 中的多个通知

ios - UICollectionView:更新节页脚标题