簡體   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