'Do something when dictionary values change

I'm looking for a way to run some code whenever the values of a dictionary changes. I'm still quite new to Swift, but this is what I have so far:

var objects: NSMutableDictionary {
    didChange(changeKind: keyValue, valuesAtIndexes: indexes, forKey: something){

    }
}

This however gives me a compiling error (Use of unresolved identifier something), and whatever I do, I can't seem to make it work. Any ideas?



Solution 1:[1]

How did you explain this:

class My {
    var dict = [String: String]() {
        didSet {
            print(oldValue)
        }
    }
}

let my = My()
my.dict["asd"] = "asd"
my.dict["bfg"] = "bfg"
my.dict = [String: String]()

Output:

[:]
["asd": "asd"]
["bfg": "bfg", "asd": "asd"]

So didSet called every time new value added.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blyabtroi