我在Swift中定义了Dictionary,其中枚举作为键和结构作为值。在运行时,我希望值添加到字典中对于给定的枚举密钥,但是我得到以下错误:使用枚举键为Swift字典添加值
“@lvalue $ T9”不等同于“(MyEnum,迈德特)”
enum MyEnum {
case A, B, C
}
struct MyData {
var x : Int
var y : Int
init(x:Int, y: Int) {
self.x = x
self.y = y
}
}
class Tester {
let myDictionary = [MyEnum : MyData]()
func dummy() {
self.myDictionary[MyEnum.A] = MyData(x: 1, y: 2) // <-- error in this line
}
}
任何想法如何正确地做到这一点?
可能重复:http://stackoverflow.com/questions/24562357/how-can-i-use-a-swift-enum-as-a-dictionary-key-conforming-to-equatable –
不是重复的该问题是关于与关联类型的枚举,因此不能自动排除 –
我检查了这一个,它没有演示如何在运行时向字典添加新条目 – Tamir