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
Matthew Giuliano
2,749 PointsCoding Challenge
I'm having trouble with this coding challenge could someone help me out?
Thanks
// 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
26,676 PointsHi Matthew,
You have a couple of problems with the code you've provided, they are:
- You're missing the
userCourseDataconstant that was originally there -
JSONObjectWIthDataisn't correct, you have a capitalIin the wordWithwhich needs to be lowercase - You're referencing
dataObjectinstead ofuserCourseData - You don't need to unwrap the
NSDataobject as it's already unwrapped inuserCourseData -
optionshould be pural, i.eoptions - You haven't downcasted
userCourseDictionaryas anNSDictionarywhich 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!
Ezekiel Elin
3,839 PointsI 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.