2016-11-23 16 views
0

我對Swift非常陌生,我在構建利用來自openweathermap.org網站的API的天氣應用程序時遇到了問題。當用戶進入城市並點擊「SUBMIT」時,他們應該能夠看到顯示天氣描述的標籤。不能在非任意值類型'任何'上使用可選鏈SWIFT

在JSON的結果是:

(
     { 
     description = haze; 
     icon = 50d; 
     id = 721; 
     main = Haze; 
    }, 
     { 
     description = mist; 
     icon = 50d; 
     id = 701; 
     main = Mist; 
    } 
) 

在嘗試調試,我使用的代碼:打印(!jsonResult [「天氣」]),這讓我看到上面的JSON細節。但是,當我嘗試獲取天氣的描述時,我似乎無法使其工作。

我的目標是:我試圖獲取天氣的描述以顯示在我的應用程序中。我目前正在收到錯誤:不能使用可選的鏈接在非任意值類型'Any'上。我們將非常感謝您的幫助!

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var cityTextField: UITextField! 

    @IBOutlet weak var resultLabel: UILabel! 


    @IBAction func submit(_ sender: AnyObject) { 
     // getting a url 
     if let url = URL(string: "http://api.openweathermap.org/data/2.5/weather?q=" + (cityTextField.text?.replacingOccurrences(of: " ", with: "%20"))! + ",uk&appid=08b5523cb95dde0e2f68845a635f14db") { 

     // creating a task from the url to get the content of that url 
     let task = URLSession.shared.dataTask(with: url) { (data, response, error) in 
      if error != nil { 
       print("error") 
      } else { 
       print("no error") 
       if let urlContent = data { 
        do { 
         let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any] 
         //print(jsonResult["weather"]!) 
         if let description = jsonResult["weather"]??[0]["description"] as? String { 
          DispatchQueue.main.sync(execute:{ 
           self.resultLabel.text = description 
          }) 
         } 
        } catch { 
         print("JSON processing failed") 
        } 
       } 
      } 
     } 
     task.resume() 
     } else { 
      resultLabel.text = "Couldn't find weather for that city. Please try a different city." 
     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
    } 
+0

這裏有兩個問號(天氣之後):'jsonResult [「weather」] ?? [0] [「description」]'嘗試移除其中一個開始並查看是否讓您更進一步 – pbodsk

+0

This文章值得一讀:https://developer.apple.com/swift/blog/?id = 37(我知道你可能已經有足夠的閱讀已經完成,但本文給出 - 我認爲 - 一個很好的介紹解析JSON在Swift中使用可選的初始值設定項) – pbodsk

+0

@pbodsk感謝您的提示! –

回答

0

試試這個

let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any] 


    let weather = jsonResult["weather"] as! [[String : Any]] 
    if let description = weather[0]["description"] as? String { 

     print(description) 
    } 
+0

你的答案中的myjson變量是指什麼? – Vinodh

+0

hoops ..對不起,這是jsonResult – Rob

+0

更新於回覆 – Vinodh

0

你用這裏混淆編譯 「??」

if let description = jsonResult["weather"]??[0] 

正確的語法只是使用一個「?」

if let description = jsonResult["weather"]?[0] 

不過,你會因爲在該行得到另一個錯誤:

let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:Any] 

你說jsonResult["weather"會給你鍵入Any。不輸入數組。

所以,你需要解開像數組:

if let descriptions = jsonResult["weather"] as? [[String : Any]], let description = descriptions[0] 

等。

+0

感謝您的幫助! :-) –

相關問題