将Anyobject转换为字典

时间:2017-06-06 15:27:34

标签: arrays json swift dictionary

我收到一个json对象,格式为

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

我用Anyobject类型

绑定它
@NSManaged var type: AnyObject?

我想把它投到字典

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

但是演员表示没有成功,而且t仍然总是零

1 个答案:

答案 0 :(得分:1)

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

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