Just trying to declare an array object. Easy?

Array of basic type

Declare an array of Int objects with explicit type:

var numbers: [Int]

You can add the object initialization:

var prime: [Int] = [Int]()

But in Swift you can use the type inference and be much shorter:

var counters = [Int]()

Here come the inner class

Let say we have a class that define an inner enumeration type:

class SomeObject
{
    enum Property: String
    {
        case real = "real"
        case imaginary = "imaginary"
    }
}

Can we define an array of such an object?

var primeObjects = [SomeObject]()

That seems to be working. No error, no warning, the object is properly created in a playground.

Can we reproduce the pattern that we used with Int and define an array of Property?

var primeProperties = [SomeObject.Property]()

This uses the type inference and call the array initialization. What can go wrong in this syntax?

Well, try this and you will get the following error:

Syntax error

That seems quite strange no?

Let’s try with another syntax. Keep the déclaration and use empty array syntax to define the value, like this:

var primeProperties: [SomeObject.Property] = []

This time the syntax is valid, the object is properly defined. No more syntax error.

But could we go back to the syntax we used with array if integer? Yes we can. But you need to wrap the inner enumeration inside a type alias, like the following example:

typealias TheProperties = SomeObject.Property
var properties = [TheProperties]()

Here we are. A working array definition with the type specified in the definition.

Can someone explain me why we need to use a type alias?

Is this tied to internal implementation of the alias, or is this some kind of bug in the way the compiler interprets the syntax of inner type?