Raul Agrait
Raul Agrait

Reputation: 6018

Swift: Cannot create empty array of nested enum type

I am trying to declare an empty array of enum defined inside of another class as follows, and am getting the following error:

class OuterClass {
    enum MyEnum {
        case ThingOne
        case ThingTwo
    }
}

// Error: Invalid use of '()' to call a value of non-function type '[OuterClass.MyEnum.Type]'
var emptyEnumArray = [OuterClass.MyEnum]()
emptyEnumArray.append(.ThingOne)

However, I can declare the array as follows with no problem:

// No errors
var emptyEnumArray: [OuterClass.MyEnum] = []

This only appears to be an issue when the enum is defined inside of another class, as this works:

enum OtherEnum {
    case ThingOne
    case ThingTwo
}

var emptyArrayTwo = [OtherEnum]()
emptyArrayTwo.append(.ThingTwo)

Any thoughts on why the first snippet doesn't work? Is this a language bug?

Upvotes: 7

Views: 404

Answers (1)

Arbipher
Arbipher

Reputation: 81

I think it's a Swift bug right now. This also works.

var emptyEnumArray = Array<OuterClass.MyEnum>()

The question just recalls me to a question in apple Swift tutorial.

Upvotes: 6

Related Questions