How to access deeply nested dictionaries in Swift

后端 未结 10 895
名媛妹妹
名媛妹妹 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 04:56

    You can use the following syntax on Swift 3/4:

    if let name = data["name"] as? String {
        // name has "John"
    }
    
    if let age = data["age"] as? Int {
        // age has 30
    }
    
    if let car = data["cars"] as? [String:AnyObject],
        let car1 = car["car1"] as? String {
        // car1 has "Ford"
    }
    

提交回复
热议问题