0
私はちょうどhttp://openweathermap.org/apiから作成した新しいオブジェクトのinitで問題が発生します。私のプロジェクトはXcodeのシミュレータではうまくいきましたが、スマートフォンにアプリケーションを入れたいと思っていただけで、致命的な問題が発生しました。 作成時に、私は下付き文字のあいまいな使用をしたと言います。天気APIからインポート辞書ですからinitでの添え字のあいまいな使用
init(weatherData: [String: AnyObject]) {
city = weatherData["name"] as! String
let coordDict = weatherData["coord"] as! [String: AnyObject]
longitude = coordDict["lon"] as! Double
latitude = coordDict["lat"] as! Double
let weatherDict = weatherData["weather"]![0] as! [String: AnyObject] // Error came here.
weatherID = weatherDict["id"] as! Int
mainWeather = weatherDict["main"] as! String
weatherDescription = weatherDict["description"] as! String
weatherIconID = weatherDict["icon"] as! String
let mainDict = weatherData["main"] as! [String: AnyObject]
temp = mainDict["temp"] as! Double
humidity = mainDict["humidity"] as! Int
pressure = mainDict["pressure"] as! Int
cloudCover = weatherData["clouds"]!["all"] as! Int
let windDict = weatherData["wind"] as! [String: AnyObject]
windSpeed = windDict["speed"] as! Double
windDirection = windDict["deg"] as? Double
if weatherData["rain"] != nil {
let rainDict = weatherData["rain"] as! [String: AnyObject]
rainfallInLast3Hours = rainDict["3h"] as? Double
}
else {
rainfallInLast3Hours = nil
}
let sysDict = weatherData["sys"] as! [String: AnyObject]
country = sysDict["country"] as! String
sunrise = NSDate(timeIntervalSince1970: sysDict["sunrise"] as! NSTimeInterval)
sunset = NSDate(timeIntervalSince1970:sysDict["sunset"] as! NSTimeInterval)
}
このコードエキス値:ここで
は、私はチュートリアルから使用されるコードの一部です。解決策はありますか?
ありがとうございます。
です!ありがとう。なぜこの問題が出てきたのか知っていますか?私はそれがどのように働いたのか理解していない(1の代わりに2ステップの宣言を作成することによって)。もう一度ありがとう:) –
私は説明を追加する答えを編集しました。ディクショナリ内のキーの値は常に 'AnyObject'(オブジェクトですが何でも構いません)ですので、コンパイラは実際の型についてより多くの情報を必要とします。 – vadian
ああ、大丈夫、この仕様を知らなかった。ありがとう –