this is not a bug or a quirk in the compiler.
Optional (which may or may not be erroneous)
what happened is that Optional stores the Dictionary as an immutable object (possibly with let ). Thus, even Optional it is changed, you cannot directly modify the basic Dictionary object (without overriding the Optional object).
updateValue(forKey:) is a mutation method, you cannot call it an immutable object and therefore an error.
you can get around this by doing
var d = dict! d.updateValue(data, forKey: id)
because you are copying the dictionary into another mutable variable, which is then modified and can call the mutation method on it
but without dict = d , your change will not apply to dict , because Dictionary is a value type, it makes a copy with each assignment
related answer
Bryan chen
source share