Milad Milad - 4 years ago 128
Swift Question

Many ways of defining a Swift dictionary

In swift there are quite a few ways on defining a dictionary. So, are all of these identical?

var dic1 = Dictionary<String, Int>()

var dic2 = [String:Int]()

var dic3: Dictionary = Dictionary<String, Int>()

var dic4: Dictionary = [String:Int]()

var dic5: Dictionary<String, Int> = Dictionary<String, Int>()

var dic6: Dictionary<String, Int> = [String: Int]()

Answer Source

There are still more for example:

var dic7 : [String:Int] = [:]

but yes, they are all identical.

Basically, unless the type is not included in the part right from the equation sign, type annotations in declaration lines are not needed because the compiler can infer the type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download