Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional dictionaries possible in Swift?

Tags:

swift

Just learning Swift, coming from PHP. I'm trying to figure out if you can do deeply nested arrays in Swift. Here's a PHP example of what I'm talking about:

$myArray = array(

"motorcycles" => array ("Honda", "Ducati", "Yamaha"),
"cars" => array(
                "sedans" => array("Jetta", "Taurus", "Impala"),
                "sport" => array("Porsche", "Ferarri", "Corvette"),
                "trucks" => array (
                                "shortbed" => array("Ford F150", "Dodge Ram 1500"),
                                "longbed" => array(
                                                    "standardCab" => array("Ford F350", "Dodge Ram 2500"),
                                                    "crewCab" => array("Ford F350", "Dodge Ram 2500")
                                                    )
                            )
            )

);
like image 382
Rick Ellis Avatar asked Nov 02 '14 01:11

Rick Ellis


People also ask

How are dictionaries implemented in Swift?

Swift dictionary is made of two generic types: Key (which has to be Hashable ) and Value. An entry can be made by providing a key and its value. A value can be retrieved by providing a key which has been inserted before. A entry can be deleted by providing a key.

What are the differences between arrays and dictionaries in Swift?

Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.

Are Dictionaries mutable Swift?

If you assign a created dictionary to a variable, then it is always mutable which means you can change it by adding, removing, or changing its items. But if you assign a dictionary to a constant, then that dictionary is immutable, and its size and contents cannot be changed.

What are dictionaries in Swift?

Swift dictionary is an unordered collection of items. It stores elements in key/value pairs. Here, keys are unique identifiers that are associated with each value.


1 Answers

Yes, in Swift that would be:

let myArray = [
    "motorcycles": ["Honda", "Ducati", "Yamaha"],
    "cars": [
        "sedans": ["Jetta", "Taurus", "Impala"],
        "sport" : ["Porsche", "Ferarri", "Corvette"],
        "trucks" : [
            "shortbed" : ["Ford F150", "Dodge Ram 1500"],
            "longbed" : [
                "standardCab":["Ford F350", "Dodge Ram 2500"],
                "crewCab":["Ford F350", "Dodge Ram 2500"]
            ]
        ]
    ]
]

Reading values from such a structure can be a bit difficult though, since Swift has difficulty discerning the types. To get the standardCab vehicles, you can do:

if let trucks = myArray["cars"]?["trucks"] as? [String:AnyObject] {
    if let standardCab = trucks["longbed"]?["standardCab"] as? [String] {
        println(standardCab)
    }
}
like image 82
vacawama Avatar answered Oct 06 '22 02:10

vacawama