【问题标题】:what are the different ways of dictionary declaration in swift [duplicate]swift中字典声明的不同方式是什么[重复]
【发布时间】:2016-09-22 16:19:33
【问题描述】:

有什么区别 1.var dict : [Int:String] = [:], 2.var dict2 : [Int:String]
3.var dict3 = Dictionary<Int,String>()

谁能帮我理解有什么区别?

【问题讨论】:

    标签: ios swift dictionary


    【解决方案1】:

    1

    var dict : [Int:String] = [:] 
    

    这会创建一个 [Int:String] 类型的空字典

    2.

    var dict2 : [Int:String] 
    

    这会创建一个 [Int:String] 类型的字典,但不会为其分配任何值。在使用之前必须给它赋值。

    3.

    var dict3 = Dictionary<Int,String>() 
    

    这只是另一种创建空字典的方法,与方法 1 相同。 Method-1 是 Method-3 的简写形式

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 2014-05-09
      • 2013-06-21
      • 2016-06-19
      • 2012-07-13
      • 2014-07-24
      • 2011-04-16
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多