简体   繁体   中英

Accessing deeply nested dictionary and array values in Swift

I have a super-nested set of dictionaries and arrays

var lines = ["set0":["lines":["Line one","Line two"],"pickone": ["set1":"Apples","set2":"Oranges"]],"set1":["lines":["Line one","Line two"],"pickone":["set3":"Bananas","set4":"Grapefruits"]]]

I have two questions:

  1. Is there any way to make the structure of those nested dictionaries more readable

  2. How can I access the values of the nested arrays

For instance, in a Playground I'm trying the following code:

lines["set0"]!["lines"]

but I'm not getting any result at all.

As to making it readable try:

var lines = [
    "set0":[
        "lines":[
            "Line one",
            "Line two"
        ],
        "pickone":[
            "set1":"Apples",
            "set2":"Oranges"
        ]
    ],
    "set1":[
        "lines":[
            "Line one",
            "Line two"
        ],
        "pickone":[
            "set3":"Bananas",
            "set4":"Grapefruits"
        ]
    ]
]

And as to your line:

lines["set0"]!["lines"]

I can confirm that it is working. Maybe you should give playground some time to display the result. Or, possibly, you forgot to:

import Foundation

Regarding your question 2: after a little change in your code, you can traverse all of it's branches.

var lines:AnyObject = ["set0":["lines":["Line one","Line two"],"pickone": ["set1":"Apples","set2":"Oranges"]],"set1":["lines":["Line one","Line two"],"pickone":["set3":"Bananas","set4":"Grapefruits"]]]

    NSLog("original object:\(lines)")
    NSLog("all keys array:\(lines.allKeys)")


    if(lines.isKindOfClass(NSDictionary)){
        for key  in lines.allKeys{
            var innerDictionary:AnyObject! = lines.valueForKey(key as! NSString as String)
            //NSLog("inner dictionary:\(innerDictionary)")
            NSLog("depth2 keys array:\(innerDictionary.allKeys)")
            if(innerDictionary.isKindOfClass(NSDictionary)){

                for innerKey  in innerDictionary.allKeys{
                    var innerValue:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)

                    if(innerValue.isKindOfClass(NSDictionary)){
                        NSLog("Depth3 keys array:\(innerValue.allKeys)")
                        var mostInnerDicionary:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)
                        NSLog("most inner Dictionary:\(mostInnerDicionary)")
                        for mostInnerKey  in mostInnerDicionary.allKeys{
                        var mostInnerValue:AnyObject! = mostInnerDicionary.valueForKey(mostInnerKey as! NSString as String)
                            NSLog("most inner Value:\(mostInnerValue)")
                        }

                    }else{
                       NSLog("innerValue:\(innerValue)")
                    }

                }
            }
        }
    }

output: run and check the output in console log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM