繁体   English   中英

在Swift中尝试访问字典嵌套数组中的键

[英]Trying to access key in Nested Array of dictionaries in Swift

也许我没有正确说出这个问题,但是我需要访问嵌套在另一个词典数组中的词典数组中的键。 我认为我得到的最接近的是

name: dict["name"] as! String,
speciesId: dict["species_id"] as! Int,
identifier: dict["identifier"] as! String,
typeId: dict["forms"]!["item0"]!["type_id"] as! Int

我可以抓住前三个变量,但是不能抓住最后一个变量,因为它嵌套在另一个字典数组中。 如果有人可以指出正确的方向来抓取type_id 当我尝试

typeId: dict["type_id"] as! Int

它只是在控制台上返回nil。

更新:因此,从这里的答案之一开始,使用此行从数组中获取一个整数是可行的

 formId: (dict["egg_groups"] as! Array<Int>)[0] as Int!

但是当尝试从数组中获取字典时,我得到EXC_BAD INSTRUCTION

试试这个并传递正确的索引,因为这里是0:

let typeId = (dict["forms"] as! Array<Dictionary<String,Any>>)[0]["type_id"]! as Int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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