2017-07-15 36 views
-1

這是我的函數:Swift3 - 無法投類型的值 '__NSArrayI'(0x107009dd8)爲 'NSDictionary中'(0x10700a2d8)

func locais(){ 
    var x: Int=0 
    let tal = "http://localhost/cmios/api/mapa/\(id1)" 
    print(tal); 
    let url1 = URL(string: tal) 
    let task1 = URLSession.shared.dataTask(with: url1!) { data, response, error in 
     guard error == nil else{ 
      print(error!) 
      return 
     } 
     guard let data = data else{ 
      print("Data is wrong") 
      return 
     } 
     let json1 = try! JSONSerialization.jsonObject(with: data, options: []) as! [String:Any] 
     let dados1 = json1["dados"] as? [[String: Any]] 

,我想返回的JSON:

[{"id_animal":"9","latitude":"41.701497","longitude":"-8.834756","nome":"testeFinal"},{"id_animal":"10","latitude":"41.701497","longitude":"-8.834756","nome":"testeFinal"}] 

這就是錯誤

Could not cast value of type '__NSArrayI' (0x107009dd8) to 'NSDictionary' (0x10700a2d8). 

任何人都可以幫忙嗎?

+0

什麼輸入? 「data」中包含的*實際*是什麼? – luk2302

+0

它已經解決了!這是一個PHP WS的錯誤,無論如何,謝謝! –

回答

0

在撥打電話JSONSerialization時擺脫as! [String:Any]。然後記錄你實際回來的類型:

let json1 = try! JSONSerialization.jsonObject(with: data, options: []) 
print ("Returned data is type \(type(of: json1)") 

這會告訴你你實際得到了什麼。

您也可以將您從數據任務中獲得的Data轉換爲字符串並記錄下來。

這聽起來好像您收到的JSON包含Array,而不是Dictionary

相關問題