Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift字典成员编译错误_Swift - Fatal编程技术网

Swift字典成员编译错误

Swift字典成员编译错误,swift,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.l

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

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
,因为如果未在
结构中指定该关键字,则属性是只读的:

mutating func add()

如果你想让你的结构能够修改它的
self
,你必须这样做:你是对的。我在“func add”之前添加了“mutating”,这就解决了它。