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) Concurrency Working With JSON

Kevin Zoltany
Kevin Zoltany
16,282 Points

Need help with Working with JSON code challenge

Link to code challenge: http://teamtreehouse.com/library/build-a-weather-app-with-swift/concurrency/working-with-json

Screenshot of error: http://snag.gy/SEtH8.jpg

Task #: 1 of 1

import Foundation

let jsonString : NSString = "{\"results\": \"success\"}"
let userCourseData : NSData = (contentsOfURL: courseURL)

let userCourseDictionary: NSDictionary =
NSJSONSerialization.JSONObjectWithData(userCourseData, options: nil, error: nil)
  as NSDictionary

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Kevin,

The code you have above doesn't have the same userCourseData variable declared as the challenge which is what's causing it to break, instead of;

let userCourseData : NSData = (contentsOfURL: courseURL)

You want.

let userCourseData : NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!

So your final code should be.

import Foundation

let jsonString : NSString = "{\"results\": \"success\"}"
let userCourseData : NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!

// Now you just need to decode it
let userCourseDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(userCourseData, options: nil, error: nil) as NSDictionary
Joshua Michaels
Joshua Michaels
7,306 Points

As of Xcode 6.3 and the latest Swift (May 2015), the decode line should be:

let userCourseDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(userCourseData, options: nil, error: nil) as! NSDictionary

Note the downcast after the "as".

**However, if you type that in the challenge it will fail as it hasn't been updated but just FYI.