You can't use [:]
unless type information is available.
You need to provide it explicitly in this case:
var dict = Dictionary<String, String>()
var
means it's mutable, so you can add entries to it.
Conversely, if you make it a let
then you cannot further modify it (let
means constant).
You can use the [:]
shorthand notation if the type information can be inferred, for instance
var dict = ["key": "value"]
// stuff
dict = [:] // ok, I'm done with it
In the last example the dictionary is known to have a type Dictionary<String, String>
by the first line. Note that you didn't have to specify it explicitly, but it has been inferred.