当前位置: 动力学知识库 > 问答 > 编程问答 >

variables - 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]()

网友答案:

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.

网友答案:

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
分享给朋友:
您可能感兴趣的文章:
随机阅读: