The following:
var emptyArray = [String]();
What do I need the parentheses () for? [String] says, that the array accepts strings only. But why do I have to put the () there?
Two things are true in Swift:
T
in Swift you write code T()
.[String]
Combine the two and you get [String]()
var emptyArray = [String]();
instantiates an object of the type emptyArray and emptyArray is a array of strings?
T()
is a shorthand for T.init()
. [String]
is a shorthand for Array<String>
. Thus, [String]()
is a shorthand for Array<String>.init()
String
. The round things afterward are parentheses, or just "parens" if you're being informal.[String]
denotes a type: array of strings, but you're not trying to assign a type toemptyArray
; you're trying to assign an instance of that type, which is where the parens come in. (Note: if any actual Swift devs see this and I'm way off, bear in mind the above: I've never used Swift. Just making an educated guess here.)