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

Swift: Making a network call

Following the steps in the video of http://teamtreehouse.com/library/build-a-weather-app-with-swift/pulling-data-from-the-web/making-a-network-call

This newline of code

let weatherData = NSData.dataWithContentsOfURL(forecastURL, options: nil, error: nil)

creates an error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSPlaceholderData initWithContentsOfURL:options:error:]: nil URL argument'

It points to the following line in AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

Hi Jonathan,

Could you please post your forecastURL and baseURL variables you have as the error is saying you don't have an instance of NSURL assigned to forecastURL.

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)")
let forecastURL = NSURL(string: "32.302452, -80.975017", relativeToURL: baseURL)

3 Answers

So this error is actually becoming pretty common now and it's because the NSURL constructor doesn't accept spaces in the string parameter which is something that unless you know about it doesn't make itself known as an actual error. Simply change your code so the lat,lng value you have has no space after the comma like below.

let forecastURL = NSURL(string: "32.302452,-80.975017", relativeToURL: baseURL)

Thanks that did the trick. Funny thing is that I am printing out the data

println(weatherData) 

and I get nil.

Chris, you're a boss! How did you find the answer?

Hi Alexander,

I ran into the same issue initially when the course was released and simply eliminated the coordinates string and replace it with an empty string and that did the trick, after some light reading I found that NSURL constructors don't accept spaces unless they're URL encoded.

So was there something in the error message that made it clear to you that you should try eliminating the coordinates string in the first place or did you just start moving things around?

Normally there's a faint orange/tanned arrow pointing up that sits beneath the area of the code where the error is, look for that and you can see what's going wrong from that point of the code onwards.

Cool. Thanks for being so helpful!