How to access deeply nested dictionaries in Swift

大憨熊 提交于 2019-12-27 17:03:47

问题


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 ten types of bugs, each with ten patterns, each pattern having ten colors. So there are 1000 unique bugs possible, and I want to track how many of each of these types the player has. The nested dictionary looks like:

var colorsDict: [String : Int]
var patternsDict: [String : Any] // [String : colorsDict]
var bugsDict: [String : Any] // [String : patternsDict]

I do not get any errors or complaints with this syntax.

When I want to increment the player's bug collection though, doing this:

bugs["ladybug"]["spotted"]["red"]++

I get this error: String is not convertible to 'DictionaryIndex< String, Any >' with the error's carrot under the first string.

Another similar post suggested using "as Any?" in the code, but the OP of that post only had a dictionary one deep so could do that easily with: dict["string"] as Any? ...

I am not sure how to do this with a multilevel dictionary. Any help would be appreciated.


回答1:


When working with dictionaries you have to remember that a key might not exist in the dictionary. For this reason, dictionaries always return optionals. So each time you access the dictionary by key you have to unwrap at each level as follows:

bugsDict["ladybug"]!["spotted"]!["red"]!++

I presume you know about optionals, but just to be clear, use the exclamation mark if you are 100% sure the key exists in the dictionary, otherwise it's better to use the question mark:

bugsDict["ladybug"]?["spotted"]?["red"]?++

Addendum: This is the code I used for testing in playground:

var colorsDict = [String : Int]()
var patternsDict =  [String : [String : Int]] ()
var bugsDict = [String : [String : [String : Int]]] ()

colorsDict["red"] = 1
patternsDict["spotted"] = colorsDict
bugsDict["ladybug"] = patternsDict


bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 1
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 2
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 3
bugsDict["ladybug"]!["spotted"]!["red"]! // Prints 4



回答2:


Another option: You could try calling dict.value( forKeyPath: "ladybug.spotted.red" )!


So I just tried this with Swift 5:

import Foundation

var d = [ "ladybug" : [ "spotted" : [ "red" : 123 ] ] ] as [String:Any]

(d as NSDictionary).value(forKeyPath: "ladybug.spotted.red")

and it works, but this is probably the best way:

d["ladybug"]?["spotted"]?["red"]



回答3:


My primary use case was reading ad-hoc values from a deep dictionary. None of the answers given worked for me in my Swift 3.1 project, so I went looking and found Ole Begemann's excellent extension for Swift dictionaries, with a detailed explanation on how it works.

I've made a Github gist with the Swift file I made for using it, and I welcome feedback.

To use it, you can add the Keypath.swift into your project, and then you can simply use a keyPath subscript syntax on any [String:Any] dictionary as follows.

Considering you have a JSON object like so:

{
    "name":"John",
    "age":30,
    "cars": {
        "car1":"Ford",
        "car2":"BMW",
        "car3":"Fiat"
    }
}

stored in a dictionary var dict:[String:Any]. You could use the following syntax to get to the various depths of the object.

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

Note that the extension supports writing into nested dictionaries as well, but I haven't yet used this.

I still haven't found a way to access arrays within dictionary objects using this, but it's a start! I'm looking for a JSON Pointer implementation for Swift but haven't found one, yet.




回答4:


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



回答5:


If it's only about retrieval (not manipulation) then here's a Dictionary extension for Swift 3 (code ready for pasting into Xcode playground) :

//extension
extension Dictionary where Key: Hashable, Value: Any {
    func getValue(forKeyPath components : Array<Any>) -> Any? {
        var comps = components;
        let key = comps.remove(at: 0)
        if let k = key as? Key {
            if(comps.count == 0) {
                return self[k]
            }
            if let v = self[k] as? Dictionary<AnyHashable,Any> {
                return v.getValue(forKeyPath : comps)
            }
        }
        return nil
    }
}

//read json
let json = "{\"a\":{\"b\":\"bla\"},\"val\":10}" //
if let parsed = try JSONSerialization.jsonObject(with: json.data(using: .utf8)!, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<AnyHashable,Any>
{
    parsed.getValue(forKeyPath: ["a","b"]) //-> "bla"
    parsed.getValue(forKeyPath: ["val"]) //-> 10
}

//dictionary with different key types
let test : Dictionary<AnyHashable,Any> = ["a" : ["b" : ["c" : "bla"]], 0 : [ 1 : [ 2 : "bla"]], "four" : [ 5 : "bla"]]
test.getValue(forKeyPath: ["a","b","c"]) //-> "bla"
test.getValue(forKeyPath: ["a","b"]) //-> ["c": "bla"]
test.getValue(forKeyPath: [0,1,2]) //-> "bla"
test.getValue(forKeyPath: ["four",5]) //-> "bla"
test.getValue(forKeyPath: ["a","b","d"]) //-> nil

//dictionary with strings as keys
let test2 = ["one" : [ "two" : "three"]]
test2.getValue(forKeyPath: ["one","two"]) //-> "three"



回答6:


Unfortunately none of these methods worked for me, so I built my own to use a simple string path like "element0.element1.element256.element1", etc. Hope this save a time for others. (just use a dots between name of elements in string)

Json example:

{
    "control": {
        "type": "Button",
        "name": "Save",
        "ui": {
            "scale": 0.5,
            "padding": {
                "top": 24,
                "bottom": 32
            }
        }
    }
}

Step 1, convert json String to Dictionary

static func convertToDictionary(text: String) -> [String: Any]? {
        if let data = text.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            } catch {
                print(error.localizedDescription)
            }
        }
        return nil
    }

Step 2, helper to get a nested objects

//path example: "control.ui.scale"
    static func getDictValue(dict:[String: Any], path:String)->Any?{
        let arr = path.components(separatedBy: ".")
        if(arr.count == 1){
            return dict[String(arr[0])]
        }
        else if (arr.count > 1){
            let p = arr[1...arr.count-1].joined(separator: ".")
            let d = dict[String(arr[0])] as? [String: Any]
            if (d != nil){
                return getDictValue(dict:d!, path:p)
            }
        }
        return nil
    }

Step 3, use helper

let controlScale = getDictValue(dict:dict, path: "control.ui.scale") as! Double?
print(controlScale)

let controlName = getDictValue(dict:dict, path: "control.name") as! String?
print(controlName)

Returns

0.5
Save



回答7:


The Swift 4 default: subscript for Dictionaries makes makes updating values in nested Dictionaries much more concise.

Get and Set a default value rather than dealing with optionals:

var dict = [String : [String : String]]()
dict["deep", default: [:]]["nested"] = "dictionary"

print(dict)
// ["deep": ["nested": "dictionary"]]

https://swift.org/blog/dictionary-and-set-improvements/




回答8:


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"
}



回答9:


Yet another approach using various overloaded Dictionary subscript implementations:

let dict = makeDictionary(fromJSONString:
        """
        {
            "control": {
                "type": "Button",
                "name": "Save",
                "ui": {
                    "scale": 0.5,
                    "padding": {
                        "top": 24,
                        "bottom": 32
                    }
                }
            }
        }
        """)!

dict[Int.self, ["control", "ui", "padding", "top"]] // 1
dict[Int.self, "control", "ui", "padding", "top"]   // 2
dict[Int.self, "control.ui.padding.top"]        // 3

And the actual implementations:

extension Dictionary {
    // 1    
    subscript<T>(_ type: T.Type, _ pathKeys: [Key]) -> T? {
        precondition(pathKeys.count > 0)

        if pathKeys.count == 1 {
            return self[pathKeys[0]] as? T
        }

    // Drill down to the innermost dictionary accessible through next-to-last key
        var dict: [Key: Value]? = self
        for currentKey in pathKeys.dropLast() {
            dict = dict?[currentKey] as? [Key: Value]
            if dict == nil {
                return nil
            }
        }

        return dict?[pathKeys.last!] as? T
    }

    // 2. Calls 1
    subscript<T>(_ type: T.Type, _ pathKeys: Key...) -> T? {
        return self[type, pathKeys]
    }
}

extension Dictionary where Key == String {
    // 3. Calls 1
    subscript<T>(_ type: T.Type, _ keyPath: String) -> T? {
        return self[type, keyPath.components(separatedBy: ".")]
    }
}

func makeDictionary(fromJSONString jsonString: String) -> [String: Any]? {
    guard let data = jsonString.data(using: .utf8)
        else { return nil}
    let ret = try? JSONSerialization.jsonObject(with: data, options: [])
    return ret as? [String: Any]
}


来源:https://stackoverflow.com/questions/25475463/how-to-access-deeply-nested-dictionaries-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!