Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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