1

When I make a dictionary a member, assignment doesn't compile:

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;
}

What is it about membership that breaks the compilation? I don't get this error if I put that line in init() FWIW.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47

1 Answers1

2

You need to add mutating from the function declaration like this because properties are readonly if you don't specify that keyword in struct:

mutating func add()
Lucas Huang
  • 3,998
  • 3
  • 20
  • 29