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 trialTom Hill
14,232 PointsStormy new NSData format optional issue
Hello, I'm having an issue with the Build a Weather App in Swift - Making a Network Call video where Xcode is returning an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
and it's happening because of this line:
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
that was updated in the 6.1 version of Xcode which I replaced for the original line like as the teacher's notes instructed.
I might just be me doing something wrong (it probably is) however any help would be appreciated!
Thanks, Tom Hill
P.S my entire code it the viewDidLoad method looks like this:
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
4 Answers
Stone Preston
42,016 PointsI dont really see anything wrong with your code, so print out the URL and verify that its nil:
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)
println("URL: \(forecastURL)")
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
if it indeed comes back as nil double check your API key
Bruno Paes Leme
1,796 PointsI have the same issue. Even after trying the same as you did, I still get the same problem. Anything else that could be cause the problem?
Colton Ballinger
1,767 PointsI had the same problem as well. However, check to make sure the forecastURL coordinates have no space in between the commas.
For example, below is code that was causing the compile error before I fixed it. Notice the space between 37.8267 and -122.423. I just had to close that space and the problem was resolved.
let forecastURL = NSURL(string: "37.8267, -122.423", relativeToURL: baseURL)
Jose Sanchez-Miranda Ros
1,736 PointsColton, that totally worked for me. Thanks!
Anand P
893 PointsTom Hill, it is really weird, I put the println fpr URL and bang, that made it bring back data instead of nil.
Tom Hill
14,232 PointsTom Hill
14,232 PointsHa... that's really weird - as soon as I add that println, it works - then I take it out again, still works... strange.
Anywho - that fixed it!
Thank you!
Stone Preston
42,016 PointsStone Preston
42,016 Pointsstrange, might have been an issue with forecast or something. maybe it took a while for your API key to go through or something i dont know. oh well.
Michael Baker
5,709 PointsMichael Baker
5,709 PointsHad same issue - removing the space after the comma in the latlong data did the trick.