Swift enum values ​​not available

I have the following class with the enumeration 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, for example:

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

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

Any ideas what I'm doing wrong here?

+4
source share
2 answers

The problem is what Typeis the Swift keyword, and your custom one is Typeconfused by the compiler.

In my tests on the playground, your code generated the same error. The solution is to change Typefor 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.

+5
source

enum - : Swift .Type :

if childMirror.valueType is String.Type {  
  println("property is of type String")
}

- .

+2

Source: https://habr.com/ru/post/1618252/


All Articles