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 trialNathan Wilkins
4,554 PointsWhen running the app it returns nil
I have looked through some of the previous questions and answers and looking at my code I can't see what is wrong with it.
When running it in a playground it still returns nil but looking at the Console Output there seems to be an issue in that the HTTP load failed.
private let apiKey = "2d4c1bdf29ae4484a321809f17582e8a"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let baseURL = NSURL(string: "https://api.forecast.io/\(apiKey)/")
let forecastURL = NSURL(string: "51.620345,0.045681", relativeToURL: baseURL)
let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
println(weatherData)
}
Any ideas?
2 Answers
Stone Preston
42,016 Pointsthe forecast API docs state that an api call looks like this:
as you can see, your base URL is:
you are missing the /forecast/ part of the URL. add that into your base URL:
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
Nathan Wilkins
4,554 PointsWhoa man. Thanks Stone.
I knew it would be something silly like that! I did have it working earlier, I must have removed it at some point.
Thanks again.
Stone Preston
42,016 Pointsno problem glad I could help
Michael Psoinos
8,178 PointsMichael Psoinos
8,178 PointsThank you Stone. I had the same exact issue.