ios – Find out how to replace the worth in dictionary as baby tree in Swift?

0
114


I wang to create an information storage because the snapshot of firebase, and utilizing the dictionary. However the worth cannot be replace into the kid of dictionary.

var take a look at:[String: Any?] = ["c1": ["c2": ["c3": 5]]]

class JSON {
    var dict:[String: Any?]
    
    init( _ val:inout [String: Any?]) {
        dict = val
    }
    
    func baby(key: String) -> JSON {
        var json = dict[key] as! [String: Any?]
        return JSON(&json)
    }
    
    func setValue(key:String, worth:Any?) {
        dict[key] = worth
    }
    
    func getInteger(key:String) ->Int {
        return dict[key] as! Int
    }
}

var json = JSON(&take a look at)

// Replace c3 = 27
json.baby(key: "c1").baby(key: "c2").setValue(key: "c3", worth: 27)

print("JSON: (json.baby(key: "c1").baby(key: "c2").getInteger(key: "c3"))")
// However c3 = 5