【问题标题】:Swift 2 - Using a Set as value type in a dictionarySwift 2 - 在字典中使用 Set 作为值类型
【发布时间】:2016-01-04 12:10:30
【问题描述】:

我是 Swift/iOS 新手,在字典中使用 Set 时遇到问题。我有以下代码:

internal let attributeLists = Dictionary<String,Set<String>>()

public func getAttributeList(name: String) -> Set<String> {
        if self.attributeLists[name] == nil{
            self.attributeLists[name] = Set<String>()
        }
        return self.attributeLists[name]!
}

编译器在self.attributeLists[name] = Set&lt;String&gt;() 处给我一个错误

它说不能分配给这个表达式的结果

想知道它是否与可选性有关。 当我使用self.attributeLists.updateValue(value: Set&lt;String&gt;(), forKey: name) 时,它会给我错误:Immutable value of type Dictionary&lt;String,Set&lt;String&gt;&gt; only has mutating members named updateValue

有人有想法吗?提前致谢。

【问题讨论】:

    标签: ios dictionary set swift2


    【解决方案1】:

    您将attributeLists 声明为let,这意味着它是一个常量且不可变的空字典。

    【讨论】:

      猜你喜欢
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 2015-08-13
      • 2017-07-16
      • 2016-09-26
      相关资源
      最近更新 更多