I am trying to get the weather for a city using Openweathermap using this code:
func getWeatherByCity(city: String) {
if let weatherRequestURL = NSURL(string: "\(openWeatherMapBaseURL)?APPID=\(openWeatherMapAPIKey)&q=\(city)") {
getWeather(weatherRequestURL: weatherRequestURL)
}
}
(The full tutorial http://www.globalnerdy.com/2016/04/02/how-to-build-an-ios-weather-app-in-swift-part-1-a-very-bare-bones-weather-app/)
The api works fine with city names that don't contain spaces.
even on the home page http://openweathermap.org/ , looking for san francisco gives no results.
What's missing here?
Following @SaintThread suggestion, removing spaces is done by replacing occurrences of the spaces with an empty String:
containsPlacemark.locality?.replacingOccurrences(of: " ", with: "")
But after contacting the support team, they recommended using underscores instead of spaces. Here is an excerpt from their email:
Indeed, spaces support is broken. Please just replace them with underscores. Indeed, you can just omit spaces spelling “San Francisco” as “SanFrancisco” but this manner can cause unexpected results with some particular cities, “San_Francisco” is the best form.
So the correct way to deal with this is the following:
containsPlacemark.locality?.replacingOccurrences(of: " ", with: "_")