0

I have an enum with 4 objects inside. I then passed one to a variable. I'm trying to create a switch statement to see which object was passed. Here is my code:

enum Collection:Int{
        case First=1, Second, Third, Fourth
}
var myCollection : Collection!

// Later on...
myCollection = Collection.Second

// Later on...
switch self.myCollection {
    case .Second:
    println("Second")
}

But I get the following error:

Enum case 'Second' not found in type 'myViewController.Collection!'

What am I doing wrong, and how can I fix it?

Jessica
  • 9,379
  • 14
  • 65
  • 136

1 Answers1

1

A bit odd and it seems the error-message is not particularly helpful. The fix nevertheless is one of the below.

  1. change var myCollection : Collection! to var myCollection : Collection.
  2. switch myCollection as Collection or the short-form switch myCollection!

The reason is that implicitly unwrapped myCollection is not of the type Collection but actually of the type Collection!.

T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32