Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Data Modeling With Structures Cleaning Up Our Date

Hunter Garrett
Hunter Garrett
6,339 Points

Receiving 'Any Object?' is not convertible to ' NSDictionary'

Swift is suggesting I downcast the first line to

let currentWeather = weatherDictionary["currently" as! NSDictionary.

However, when I downcast that I get erros for the rest of the "as" instances...

CURRENT CODE:

let currentWeather = weatherDictionary["currently"] as NSDictionary

    currentTime = currentWeather["time"] as Int
    temperature = currentWeather["temperature"] as Int
    humidity = currentWeather["humidity"] as! Double
    precipProbability = currentWeather["precipProbability"] as Double
    summary = currentWeather["summary"] as String
    icon = currentWeather["icon"] as String

Any luck with this? Found myself in the same situation just now. I'm pretty sure it's an issue with the line:

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler:...

xCode keeps prompting me to force a downcast on forecastURL with the syntax:

forecastURL!

which I think ultimately forces the dataObject to have a problem.

Not sure how to proceed.

3 Answers

Laura Morgan
Laura Morgan
1,941 Points

Dang. I am getting the same thing. Did you happen to figure it out?

Dominykas Semeklis
Dominykas Semeklis
2,414 Points

Try adding ! with each as. I reckon this has changed when xcode was updated. My code:

init(weatherDictionary: NSDictionary) {
        let currentWeather = weatherDictionary["currently"] as! NSDictionary

        currentTime = currentWeather["time"] as! Int
        temperature = currentWeather["temperature"] as! Int
        humidity = currentWeather["humidity"] as! Double
        precipProbability = currentWeather["precipProbability"] as! Double
        summary = currentWeather["summary"] as! String
        icon = currentWeather["icon"] as! String

    }

I found you need to unwrap each value in that statement not just forecastURL

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completeHandler: { (location: NSURL!, response: NSURLRespones!, error: NSError!) ->Void