Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将任何对象强制转换为字典_Arrays_Json_Swift_Dictionary - Fatal编程技术网

Arrays 将任何对象强制转换为字典

Arrays 将任何对象强制转换为字典,arrays,json,swift,dictionary,Arrays,Json,Swift,Dictionary,我收到一个具有这种格式的json对象 "type":[{"id":"926"}] 我用Anyobject类型绑定它 我想把它编入字典 if let t = type as? Dictionary<String, AnyObject> { print(t["id"]) } 但是cast没有成功,t仍然总是niljson响应中的type值是一个字典数组,因此您应该相应地解析它 if let t = type as? [[String:AnyObject]] {

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

"type":[{"id":"926"}]
我用Anyobject类型绑定它

我想把它编入字典

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

但是cast没有成功,t仍然总是nil

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

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