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) Pulling Data From the Web Fetching JSON Data

Code 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
Ben Griffith
5,808 Points

Hey, 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)

thank 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

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)

```Swift
This Works!
Nicolas Pasut
Nicolas Pasut
1,796 Points

Why hasn't been this updated in the courses? I lost value time trying to find this! Thanks for the answers :)

Haven'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)