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

Coding Challenge

I'm having trouble with this coding challenge could someone help me out?


// We've already created a NSData object with some json encoded data for you
import Foundation

let jsonString : NSString = "{\"results\": \"success\"}"
let userCourseDictionary : NSDictionary = NSJSONSerialization.JSONObjectWIthData(dataObject!, option: nil, error: nil)

// Now you just need to decode it

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Matthew,

You have a couple of problems with the code you've provided, they are:

  1. You're missing the userCourseData constant that was originally there
  2. JSONObjectWIthData isn't correct, you have a capital I in the word With which needs to be lowercase
  3. You're referencing dataObject instead of userCourseData
  4. You don't need to unwrap the NSData object as it's already unwrapped in userCourseData
  5. option should be pural, i.e options
  6. You haven't downcasted userCourseDictionary as anNSDictionary which is required
let jsonString : NSString = "{\"results\": \"success\"}"
let userCourseData : NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
let userCourseDictionary = NSJSONSerialization.JSONObjectWithData(userCourseData, options: nil, error: nil) as NSDictionary

Happy coding!

I believe you need to cast it as an NSDictionary, then do

if let key = userCourseDictionary["key"] {
    //Some Code

Note that in Swift 1.2 Beta, you can put multiple let statements in one if block, to prevent unneeded indenting.