2017-02-17 2 views
-1

私はAPI、特にopenweathermap apiで遊んでいたチュートリアルに従っていました。問題に遭遇し、xCodeは "Fix it with and and ??"というオプションを教えてくれました。いずれかの問題。オプションの型 'NSDictionary ??'の値アンラップされていない

if let description = ((jsonResult?["weather"] as? NSArray)?[0] as? NSDictionary)?["description"] as? Stringにエラーが発生しました。オプションの値 'NSDictionary ??'巻き戻されていないエラー。

答えて

1

単にNSArrayの代わりにSwiftのネイティブタイプの配列を使用してください。

do { 
    if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [String: Any] { 
     if let weatherArray = jsonResult["weather"] as? [[String:Any]], 
      let dic = weatherArray.first, let description = dic["description"] as? String { 
      print(description) 
     } 
    } 

} catch {  
    print("JSON Processing Fail")       
} 
関連する問題