I wonder if you can write below:
var t = [String:[Int]]()
if t["a"] == nil { t["a"] = [1] }
else { t["a"]!.append(2) }
This assigns a hash value if it does not exist and reuses it yet.
I know that I can write a function like
func extend(inout t:[String:[Int]], key:String, n:Int) {
if t[key] == nil { t[key] = [n] }
else { t[key]!.append(n) }
}
but wonder if there is a more elegant way.
Edit Actually, I was looking for a shortcut for the function, but I copied / pasted the test code above with different init / add values.
source
share