json - Swift:为什么我的条件绑定(bind)不起作用?

标签 json swift

我从 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/

相关文章:

ios - TableView 自定义单元格设计问题

ios - Xcode 7.3 UITableViewController 单元格未正确显示

ios - 在 UICollectionViewCell 中添加带计时器的倒计时标签的最佳方法,即使在单元格被重用后也能正常工作

ios - 解析 json 时出现结束错误时出现 Alamofire 垃圾

javascript - 通用JSON调用类

json - 通过 jq 从字符串中解析浮点值

javascript - 将 JSON 转换为无序 HTML 列表(DFS?)

json - 将 FIRTimestamp 转换为 JSON

ios - IBDesignable 绘图未显示在 Storyboard 中

xcode - 为什么 IB 不提供在自定义 Swift 文件中插入 @IBOutlet 或 @IBAction 的选项?