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 trialCraigg Bergeron
7,320 PointsCode Challenge Fetching JSON Data
Bummer! You need to assign the result of the NSData call to the courseData constant.
and my code is
import Foundation
let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/")
let courseURL = NSURL(string: "/course/(courseID)", relativeToURL: treehouseBaseURL)
let courseData = NSData.dataWithContentsOfURL(courseURL , options: nil , error: nil)
4 Answers
Ben Griffith
5,808 PointsHey, since XCode 6.1 there have been some changes made to Swift, including NSData.
NSData.dataWithContentsOfURL(courseURL , options: nil , error: nil)
has since been replaced by
NSData(contentsOfURL:courseURL!, options: nil, error: nil)
NB - Be sure to unwrap the optional of courseURL
So a working example would be;
import Foundation
let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/")
let courseURL = NSURL(string: "/course/\(courseID)", relativeToURL: treehouseBaseURL)
let courseData = NSData(contentsOfURL: courseURL!, options: nil, error: nil)
Anthony Price
1,412 Pointsimport Foundation
let treehouseBaseURL = NSURL(string: "https://api.teamtreehouse.com/")
let courseURL = NSURL(string: "/course/\(courseID)", relativeToURL: treehouseBaseURL)
let courseData = NSData(contentsOfURL:courseURL!, options: nil, error: nil)
```Swift
This Works!
Nicolas Pasut
1,796 PointsWhy hasn't been this updated in the courses? I lost value time trying to find this! Thanks for the answers :)
Stephen Whitfield
16,771 PointsHaven't done this course, but I'm assuming you're using string interpolation in the courseURL assigning? If so, make it
let courseURL = NSURL(string: "/course/\(courseID)", relativeToURL: treehouseBaseURL)
Craigg Bergeron
7,320 PointsCraigg Bergeron
7,320 Pointsthank you i even tried to change it to the NSData(contentsOfURL instead but it didn't want to work for me their either but probably because i forgot to unwrap so thank you
Ben Griffith
5,808 PointsBen Griffith
5,808 PointsNo problem!