Are multi-dimensional dictionaries possible in Swift?

Just learn Swift based on PHP. I am trying to figure out if you can make deeply nested arrays in Swift. Here is an example of PHP 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") ) ) ) ); 
+6
source share
1 answer

Yes, in Swift it will 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 complicated, as Swift has difficulty recognizing types. To get standardCab vehicles, you can:

 if let trucks = myArray["cars"]?["trucks"] as? [String:AnyObject] { if let standardCab = trucks["longbed"]?["standardCab"] as? [String] { println(standardCab) } } 
+7
source

Source: https://habr.com/ru/post/977596/


All Articles