Swift 2 - 在字典中使用集合作为值类型
Swift 2 - Using a Set as value type in a dictionary
我是 Swift/iOS 的新手,我在使用字典中的集合时遇到了问题。我有以下代码:
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<String>()
处给我一个错误
它说无法赋值给这个表达式的结果。
想知道它是否与可选性有关。
当我使用 self.attributeLists.updateValue(value: Set<String>(), forKey: name)
时,它会给我错误:Immutable value of type Dictionary<String,Set<String>> only has mutating members named updateValue
有人有想法吗?提前致谢。
您正在用 let
声明 attributeLists
,这意味着它是一个常量且不可变的空字典。
我是 Swift/iOS 的新手,我在使用字典中的集合时遇到了问题。我有以下代码:
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<String>()
它说无法赋值给这个表达式的结果。
想知道它是否与可选性有关。
当我使用 self.attributeLists.updateValue(value: Set<String>(), forKey: name)
时,它会给我错误:Immutable value of type Dictionary<String,Set<String>> only has mutating members named updateValue
有人有想法吗?提前致谢。
您正在用 let
声明 attributeLists
,这意味着它是一个常量且不可变的空字典。