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

Jamie Baker
Jamie Baker
5,703 Points

Why can I not see the contents of my JSON object?

Hi, this is for the stormy App. This is my code, and as you can see at the end I'm trying to print my Dictionary object. It's not appearing in the console and when I try and print it in a playground, it doesn't show up either. The playground does, however, output my NSURLSession and SessionDowloadTasks objects with an identifier, which I guess means the network call was successful? but then don't really get why I can't see the Json object.

Help much appreciated!

let baseURL = NSURL(string:"https://api.forecast.io/forecast/ccc2f537f3cebbe7049f67424695c2e7/") let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL) let sharedSession = NSURLSession.sharedSession() let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in if (error == nil) { let dataObject = NSData(contentsOfURL: location) let weatherDic: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as! NSDictionary! println(weatherDic) }

})

Jamie Baker
Jamie Baker
5,703 Points

this is the result in the playground output column:

<__NSCFLocalDownloadTask: 0x7fd3fa709810>{ taskIdentifier: 1 } { suspended }

1 Answer

Hello Jamie,

Have you compared your code to the example code provided in the course?

Here is how I have mind setup.

func getCurrentWeatherData() -> Void {

        userLocation = "\(userLatitude),\(userLongitude)"

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "\(userLocation)", relativeToURL:baseURL)

        //39.1267,-77.714 Purcellville VA (EAST COAST USA)
        //72.371224,-41.382676 GreenLand (Cold Place!)
        //\(userLocation) (YOUR LOCATION!)

        //println(userLocation)

        let sharedSession = NSURLSession.sharedSession()

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