【问题标题】:Type of expression is ambigous without more context在没有更多上下文的情况下,表达式的类型是模棱两可的
【发布时间】:2017-07-13 08:55:44
【问题描述】:

我正在尝试创建 dateAndResultsDictionary 和空字典数组,但我收到此错误。我究竟做错了什么?

【问题讨论】:

    标签: arrays swift dictionary


    【解决方案1】:

    该语句在语法上不正确。字典不能保存一种以上类型的键或值,因此类型声明如下:

    [String: [Int], String: String] 
    

    是非法的。注意 [Int]Array<Int> 在语义上是相同的。

    如果你想保存不止一种东西,可能最好的方法是使用枚举:

     enum DictionaryContent 
     {
         case string(String)
         case intArray([Int])
     }
    
     var dateAndResultDictionary = [[String: DictionaryContent]]()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多