Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift enum values are not accessible [duplicate]

I have following class with enum defined in it:

public class MyError: NSError {

    public enum Type: Int {
        case ConnectionError
        case ServerError
    }

    init(type: Type) {
        super.init(domain: "domain", code: type.rawValue, userInfo: [:])
    }
}

When I try to check the error later in my tests like:

expect(error.code).to(equal(MyError.Type.ConnectionError.rawValue))

I get the compilation error: Type MyError.Type has no member ConnectionError

Any ideas what I am doing wrong here?

like image 774
Ostap Maliuvanchuk Avatar asked Dec 02 '15 09:12

Ostap Maliuvanchuk


1 Answers

The problem is that Type is a Swift keyword and your custom Type confuses the compiler.

In my tests in a Playground your code generated the same error. The solution is to change Type for any other name. Example with Kind:

public enum Kind: Int {
    case ConnectionError
    case ServerError
}

init(type: Kind) {
    super.init(domain: "domain", code: type.rawValue, userInfo: [:])
}

Then

MyError.Kind.ConnectionError.rawValue

works as expected.

like image 197
Eric Aya Avatar answered Sep 19 '22 08:09

Eric Aya