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 trialKevin Zoltany
16,282 PointsNeed 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
26,676 PointsHi 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
7,306 PointsAs 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.
Kevin Zoltany
16,282 PointsKevin Zoltany
16,282 PointsThank you
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsYour welcome.
Radomir Stanev
2,415 PointsRadomir Stanev
2,415 PointsThanks!