I have a nested dictionary and an array containing path fragment. I need to update the value at that location.
I am possibly looking for a recursive function instead of extensions to Dictionary types and such.
I am not able to do this recursively because I making a copy of the inout param.
var dict: [String: Any] = ["channel": ["item": ["title": 1111]]]
var pathFrag = ["channel", "item", "title"]
var val = 123
func addAt(pathFrag: inout [String], val: Int, data: inout [String: Any]) {
if let first = pathFrag.first {
if let item = data[first] {
print(item)
pathFrag.remove(at: 0)
if !pathFrag.isEmpty {
var d: [String: Any] = data[first] as! [String: Any]
print("e: \(d)")
return addAt(pathFrag: &pathFrag, string: string, data: &d)
} else {
data[first] = val
print("else: \(data)") // ["title": 123]
}
}
}
}
addAt(pathFrag: &pathFrag, val: val, data: &dict)
print(dict)
How to update the value of title to 123?
var d = dict as NSDictionary; d.setValue(123, forKeyPath: "channel.item.title");, but it is givingSIGABRT.