An Empty Array with parentheses on the end?

Howdy folks - I have a rather simple syntax question to ask.

How come, when declaring an empty array like:

var emptyArray = [AnyObject]()

You need to have that set of parentheses at the end? I am aware that anything can go where AnyObject is, whether it be Int, String, etc., but can’t quite get my head around those extra parentheses.

Thanks so much for reading!

The way I understand these kinds of declarations is like this:

var emptyArray1 = [String]() means “Here is an array named emptyArray1 and right now it has been initialised but has nothing in it.” The () is an initialiser.
If you say print(emptyArray1.count) you will get 0 printed in your Console

If you open up a playground file and create a function, say

func printHello() {

then on the next line you say


and click on run, nothing will happen. You will note that the complier has recognised that you have defined the function and the line printHello has changed colour matching that of the function definition but it does nothing.

As soon as you add the initialiser parenthesis

and click on run, it will run the function and you will see Hello printed in your Console.

Does that help?

1 Like