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")
)
)
)
);
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)
}
}
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.