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
Jonathan Carpenter
10,251 PointsI'm getting a nil value for my url when I try to put in different coordinates.
I'm modifying a weather app I built along side a course here to accept my coordinates. It uses the forecast api for weather data. Here is my url code:
private let apiKey = "92a0b81218fc51a38a717d1aed2b4a22"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
refreshActivityIndicator.hidden = true
getCurrentWeatherData()
}
func getCurrentWeatherData() -> Void {
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "36.494081, -80.603625", relativeToURL: baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: {
(location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in...
So the problem is in this line here: let forecastURL = NSURL(string: "36.494081, -80.603625", relativeToURL: baseURL)
The strings of numbers are my coordinates. Theoretically this should work. The coordinates for Alcatraz Island, CA and (test case) Antartica were accepted fine and displayed weather data. Is there a reason my coordinates won't work?
Is this an issue with my code or with the API?
Thanks.
2 Answers
Chris Shaw
26,676 PointsHi Jonathan,
The issue is the space after the comma in your coordinates, NSURL objects won't accept spaces unless they're URL encoded so once you remove it your code should start working normally again.
Jonathan Carpenter
10,251 PointsWow, simple. Thanks!