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

Wayne Sang
Wayne Sang
3,157 Points

My code isn't compiling in the challenge, but it functions in my app. Possible Xcode 6.3 difference?

I understand there's already syntax differences in the provided code and what I'm using. I finished the course already but this one step is standing in the way of my completion!

json.swift
// We've already created a NSData object with some json encoded data for you
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

3 Answers

Wayne Sang
Wayne Sang
3,157 Points

Never mind!

I wrote it again and excluded the bangs, and it passed me. I needed those in my weather app, but for some reason didn't need them in the challenge.

AR Ehsan
AR Ehsan
7,912 Points

The code needed to solve the challenge is discussed around 3:20. It looks like you have the variable type and downcasting set up, but in this case there's no need to reference a url since they already stored the json string as a constant, "jsonString". Basically, on the last line after the = sign, you just need to call the NSJSONSerialization.JSONObjectWithData() method on the json object like this:

NSJSONSerialization.JSONObjectWithData(userCourseData, options: nil, error: nil) as NSDictionary PLease mark as best answer

Wayne Sang
Wayne Sang
3,157 Points

That's actually what I already have, with some syntax alterations for the new version of Swift.

Furthermore the error the challenge provides is: Bummer! You must have a variable named userCourseDictionary.

Which I certainly am. It's one reason that makes me suspect the challenge compiler isn't interpreting it correctly.

The output reads: swift_lint.swift:10:126: error: expected type after 'as' let userCourseDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(userCourseData!, options: nil, error: nil) as! NSDictionary

Which I don't fully understand, but it certainly doesn't seem to understand the new "as!" syntax for down casting introduced in Xcode 6.3.