2016-02-14 68 views
1

GET方法Alamofire我有一些問題。當我的數據我有錯誤:Alamofire扁平錯誤:意外地發現零,而解包一個可選值

EXC_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子碼=爲0x0) 致命錯誤:意外發現零而展開的可選值

我在做什麼錯?我的代碼:

 let URLString = "http://MyURLWebService..."; 
     Alamofire.request(.GET, URLString) 
     .responseJSON { (response) in 
      print(response.request) // original URL request 
      print(response.response) // URL response 
      print(response.data)  // server data 
      print(response.result) // result of response serialization 

      if let JSON = response.result.value { 
       print(JSON["Phones"] as! String) 
       //print("JSON: \(JSON)") 
      } 

    } 
+0

我的格式REST API數據: [{電話 「:」 + 1(100)111-22-33「,」Phones2「:null}] – Dim

回答

0

這是工作:

  if let jsonResult = response.result.value { 
       let Address = jsonResult[0]["Address"] 
       let Phones = jsonResult[0]["Phones"] 
       print("JSON: Address: \(Address)") 
       print("JSON: Phones: \(Phones)") 
      } 

我希望它能幫助別人

0

我覺得JSON對象與"Phones"作爲名稱的成員。所以最好使用改爲print(JSON["Phones"] as? String)

編輯:

你的JSON字符串已損壞。您必須用雙引號包裝Phones

這樣的:

[{"Phones":"+1 (100) 111-22-33","Phones2":null}]

相關問題