Many ways of defining a Swift dictionary

后端 未结 2 957
时光说笑
时光说笑 2020-12-22 04:07

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

var dic1 = Dictionary()

var dic2 = [String:         


        
相关标签:
2条回答
  • 2020-12-22 04:46

    Yes, all these 6 lines do produce the same result:

    • an empty
    • mutable
    • dictionary
    • where the key has type String
    • and the value has type Int
    0 讨论(0)
  • 2020-12-22 04:56

    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.

    0 讨论(0)
提交回复
热议问题