【发布时间】:2015-06-01 17:20:10
【问题描述】:
当我将字典设为成员时,分配不会编译:
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 中,我不会收到此错误。
【问题讨论】:
-
如果你希望你的结构能够修改它的
self,你必须这样做:stackoverflow.com/a/30477167/2227743 -
你是对的。我在“func add”之前添加了“mutating”并修复了它。
标签: swift