Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Cannot create empty array of nested enum type

Tags:

swift

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?

like image 894
Raul Agrait Avatar asked Sep 28 '15 22:09

Raul Agrait


1 Answers

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.

like image 64
Arbipher Avatar answered Nov 18 '22 19:11

Arbipher