How to update a value in a nested dictionary given path fragment in Swift?

前端 未结 2 1829
不知归路
不知归路 2021-01-21 13:52

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

2条回答
  •  旧时难觅i
    2021-01-21 14:42

    Note that var d: [String: Any] = data[first] as! [String: Any] makes a copy of data[first] and not a reference to it like & and inout. So, when addAt(pathFrag: &pathFrag, string: string, data: &d) is called, only d is changed. To make this change reflect on the original data[first], you have to assign d back to data[first].

    Do this:

    var dict: [String: Any] = ["channel": ["item": ["title": 1111]]]
    var pathFrag = ["channel", "item", "title"]
    
    func addAt(pathFrag: inout [String], data: inout [String: Any]) {
        if let first = pathFrag.first {
            if let item = data[first] {
                pathFrag.remove(at: 0)
                if !pathFrag.isEmpty {
                    var d: [String: Any] = data[first] as! [String: Any]
                    addAt(pathFrag: &pathFrag, data: &d)
                    data[first] = d
                } else {
                    data[first] = 123
                }
            }
        }
    }
    
    
    addAt(pathFrag: &pathFrag, data: &dict)
    print(dict)
    

提交回复
热议问题