【问题标题】:Cast Anyobject to dictionary将 Anyobject 转换为字典
【发布时间】:2017-06-06 15:27:33
【问题描述】:

我收到一个具有这种格式的json 对象

"type":[{"id":"926"}]

我用Anyobject 类型绑定它

@NSManaged var type: AnyObject?

我想把它转换成字典

if let t = type as? Dictionary<String, AnyObject>  {
     print(t["id"])
    }

但是演员没有成功,而且 t 仍然总是 nil

【问题讨论】:

标签: arrays json swift dictionary


【解决方案1】:

json 响应中“type”的值是一个字典数组,所以你应该相应地解析它。

if let t = type as? [[String:AnyObject]] {
   if let id = t.first?["id"] as? String {
      print(id)
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多