Swift字典成员编译错误



当我将字典作为成员时,赋值不能编译:

struct MyClass {
var lists = [String:Int]();
init() {}
func add() {
    // this compiles
    var x = [String:Int]();
    x["y"] = 3;
    // this gets the compiler error 'cannot assign to the result of this expression'
    self.lists["y"] = 3;
}

成员关系是什么破坏了编译?我不会得到这个错误如果我把这行放在init() FWIW.

您需要像这样从函数声明中添加mutating,因为如果您不在struct中指定关键字,属性是只读的:

mutating func add()

相关内容

最新更新