Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous reference to member 'init(from:)' for Enum

Tags:

enums

swift

Let's say we have this Enum:

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

I would like to init it with a Int32, so I use this:

    let myEnum = NumberEnumSpecial.init(rawValue: 2)

This works in a playground project but not in my regular App project. I'm getting this error for the exact same code:

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

Clicking on the candidates has no effect.

If you ask me there seems to be a Enum somewhere in the code with an implementation for init(from) causing this error on my Enum. But searching for this text gives me no results.

What is this error and how can find out what is causing this ?

Using Swift 3.2 and XCode9.0

Currently workaround:

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

    init?(withSpecialNumber number : Int32) {
          self.init(rawValue: number)
    }
}
like image 311
Sjoerd Perfors Avatar asked Oct 13 '17 07:10

Sjoerd Perfors


1 Answers

I had the same problem using Xcode 9.2 beta (9C32c), if it is a bug it is still not fixed in this version. I found a workaround to make the error disappear without overriding the init.

I changed this:

 NumberEnumSpecial.init(rawValue: 2)

to this:

 NumberEnumSpecial(rawValue: 2)
like image 185
Júlia Garrigós Avatar answered Nov 03 '22 01:11

Júlia Garrigós