2016-10-24 5 views
0

エラーメッセージを返します。タイプ 'Any'には、コードの最後の2行にサブスクリプトメンバがありません。私はスイフト3でNSArrayのためにこれを解決することはできません:どのような考え?サブスクリプトメンバをNSArray Swift 3にキャストする方法

let weatherDictionary: NSDictionary = (try! JSONSerialization.jsonObject(with: dataObject!, options: [])) as! NSDictionary 
_ = try JSONSerialization.jsonObject(with: dataObject!, options: .allowFragments) as! [String:Any] 

その後、

struct Weekly{ 
    var dayZeroTemperatureMax: Int 
    var dayZeroTemperatureMin: Int 

    init (weatherDictionary: NSDictionary) { 

    let weeklyWeather = weatherDictionary["daily"] as! NSDictionary 
    let weeklyForcast = weeklyWeather["data"] as! NSArray 

    //DAY ZERO 

    dayZeroTemperatureMax = weeklyForcast[0]["temperatureMax"] as! Int 
    dayZeroTemperatureMin = weeklyForcast[0]["temperatureMin"] as! Int 
    } 
    } 
+0

weatherDictionaryをNSDictionary =(try!JSONSerialization.jsonObject(with:dataObject!、options:[]))として使用します。 NSDictionary – Bux

+0

let weeklyWeather =ウィークリー(weatherDictionary:weatherDictionary) – Bux

+0

!! http://json.org/example.htmlこちらをご確認ください。 JSONとは何かを理解したいと思っています! –

答えて

1

これにコードを変更してみてください:

init (weatherDictionary: [String: Any]) { 

    if let weatheralerts = (weatherDictionary["alerts"] as? [[String: Any]]) { 

     userAlert = weatheralerts[0]["title"] as! String 

    } else { 
     userAlert = "" 
    } 
} 
:あなたは(WeatherAlertsで)あまりにもこれを変更する必要があるかもしれません、この同じプロジェクトで

let weeklyWeather = weatherDictionary["daily"] as! [String: AnyObject] 
let weeklyForcast = weeklyWeather["data"] as! [[String: Any]] 

私は本当に知りませんこれを変える理由、誰かが説明をしているなら、ここでコメントしてください。

でも同じですproject私は "致命的なエラーが発生しました:予期せぬことに、オプション値をアンラッピングしている間に見つかりませんでした"というエラーhereがありますか? URLSessionDownloadTaskについての変更点

+0

ありがとう!それはすごくうまくいった。私も説明はありませんが、それは動作します! – Bux

関連する問題