Home > Mobile >  Swift Enum case is not a member of type
Swift Enum case is not a member of type

Time:03-31

I know there are many questions named similarly, but as far as I can tell this is a new one

Here is a basic example of what I want to do, I'm not sure why it doesn't work :

enum Test  {
    case A(Int)
    case B(String)
}
let a : Test.A //Enum case 'A' is not a member of type 'Test'

But it is ! What's happening here ?

CodePudding user response:

The type is Test, not A.

let a: Test = Test.A(2)

CodePudding user response:

The type is actually Test. A and B are the cases. Actually, you should use lower case for the cases, so it should be:

enum Test  {
    case a(Int)
    case b(String)
}

You could use this enum as follows:

var myVar: Test

myVar = Test.a(0) // long version
myVar = .a(10) // short version: "Test" is inferred
myVar = .a(125)

myVar = .b("Hello")
myVar = .b("World")
  • Related