我从 http://pokeapi.co/api/v1/pokemon/1/ 读取了 JSON 文件并将它们存储到 pokemonInfoDict。
所以 pokemonInfoDict 的类型是 Dictionary < String, AnyObject >
print(pokemonInfoDict["moves"]!) has the folllow output:
(
{
"learn_type" = "tutor";
"name" = "Bind";
"resource_uri" = "/api/v1/move/20/";
},
{
"learn_type" = "machine";
"name" = "Swords-dance";
"resource_uri" = "/api/v1/move/14/";
}
)
因此,它是一个[Dictionary < String, String >]类型
那么,为什么我的条件绑定(bind)无法将其转换为 [Dictionary < String, String >] 类型? 未调用 print(movesArray)。
if let movesArray = pokemonInfoDict["moves"] as? [Dictionary<String,String>] where movesArray.count > 0
{
print(movesArray)
}
如有任何帮助,我们将不胜感激。我已经坚持了很长一段时间......
最佳答案
试试这个,
if let movesArray = pokemonInfoDict["moves"] as? [[String:AnyObject]] where movesArray.count > 0
{
print(movesArray)
}
关于json - Swift:为什么我的条件绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35576464/