ios – The way to replace the price in dictionary as kid tree in Swift?

I wang to create a data storage as the snapshot of firebase, and using the dictionary. But the value can not be update into the child of dictionary.

var test:[String: Any?] = ["c1": ["c2": ["c3": 5]]]

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

var json = JSON(&test)

// Update c3 = 27
json.child(key: "c1").child(key: "c2").setValue(key: "c3", value: 27)

print("JSON: \(json.child(key: "c1").child(key: "c2").getInteger(key: "c3"))")
// But c3 = 5

Leave a Reply

Your email address will not be published. Required fields are marked *