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
jonathanbello2
4,224 PointsSwift: 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 {
jonathanbello2
4,224 Pointslet baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)")
let forecastURL = NSURL(string: "32.302452, -80.975017", relativeToURL: baseURL)
3 Answers
Chris Shaw
26,676 PointsSo 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)
jonathanbello2
4,224 PointsThanks that did the trick. Funny thing is that I am printing out the data
println(weatherData)
and I get nil.
agrizzle
1,400 PointsChris, you're a boss! How did you find the answer?
Chris Shaw
26,676 PointsHi 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.
agrizzle
1,400 PointsSo 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?
Chris Shaw
26,676 PointsNormally 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.
agrizzle
1,400 PointsCool. Thanks for being so helpful!
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsHi Jonathan,
Could you please post your
forecastURLandbaseURLvariables you have as the error is saying you don't have an instance ofNSURLassigned toforecastURL.