How to access deeply nested dictionaries in Swift

后端 未结 10 930
名媛妹妹
名媛妹妹 2020-11-28 04:46

I have a pretty complex data structure in my app, which I need to manipulate. I am trying to keep track of how many types of bugs a player has in thier garden. There are te

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 05:03

    I had the same issue, where I wanted to get boolValue nested in dictionary.

    {
      "Level1": {
        "leve2": {
          "code": 0,
          "boolValue": 1
        }
      }
    }
    

    I tried a lot of solution but those didn't worked for me as i was missing type casting. So I used following code to get the boolValue from json, where json is a nested dictionary of type [String:Any].

    let boolValue = ((json["level1"]
        as? [String: Any])?["level2"]
        as? [String: Any])?["boolValue"] as? Bool
    

提交回复
热议问题