2017-06-29 46 views
0

任何人都可以告訴我如何解決這個問題嗎?我只是試圖從事情發言接收信號。條件綁定的初始值設定項必須有可選類型,而不是'[String:Any]'

`self.title = "Home" 
    print("Requesting data...") 
    Alamofire.request("https://api.thingspeak.com/channels/290427/feeds.json", parameters: ["results": "1", "location": "false"]) // Gets the latest info from ThingSpeak 
     .responseJSON { response in 

      print("Data downloaded: \(response.result)") 
      if let json = response.result.value as! [String:Any] { 
       print(json) //see full data 

       if let feeds = json["feeds"] as? [String: Any] { 

        for feed in feeds { 
         print(feed["field2"]) 
         if let temperatureStr = feed["field2"] as? String, let dateStr = feed["created_at"] as? String { 
          if let temperature = Double(temperatureStr){ 
           self.label.text = "Temperature: \(temperature)°F" //Displays last updated data entry 

          } 

的錯誤是在該行

if let json = response.result.value as! [String:Any] { 

錯誤消息稱「初始值設定條件結合必須有可選的類型,而不是‘[字符串:任何]’

+2

更換'的'的'的!? '(如你在下面2行)。 - 順便說一句,你的問題標題甚至不符合錯誤消息。 –

回答

2

如果你想使用條件表達式的右側應該是可選的。

更改此:

if let json = response.result.value as! [String:Any] 

要這樣:

if let json = response.result.value as? [String:Any] 
+0

這些不起作用。進一步向下給我幾個構建錯誤。 –

+0

[繼承人存在構建錯誤時,我把它放在?] [1] [1]:https://i.stack.imgur.com/tKnua.png –

+0

@ColinH答案是正確的。這些新錯誤與原始問題無關。代碼中有一堆語法錯誤(例如'DateFormatter()'而不是'DateFormatter'),你應該修正它們。 –

0

該消息意味着,你需要有可選的類型,因此只要改變

if let json = response.result.value as! [String:Any] {

if let json = response.result.value as? [String:Any] { 
相關問題