我正在尝试使用此代码使用Openweathermap获取城市的天气:
func getWeatherByCity(city: String) {
if let weatherRequestURL = NSURL(string: "\(openWeatherMapBaseURL)?APPID=\(openWeatherMapAPIKey)&q=\(city)") {
getWeather(weatherRequestURL: weatherRequestURL)
}
}
api适用于不包含空格的城市名称。
即使在主页http://openweathermap.org/,寻找旧金山也没有结果。
这里缺少什么?
在@SaintThread建议之后,删除空格是通过用空字符串替换空格的出现来完成的:
containsPlacemark.locality?.replacingOccurrences(of: " ", with: "")
但在与支持团队联系后,他们建议使用下划线而不是空格。以下是他们的电子邮件的摘录:
实际上,空间支持被打破了。请用下划线替换它们。实际上,您可以省略拼写“旧金山”作为“SanFrancisco”的空格,但这种方式可能会导致一些特定城市的意外结果,“San_Francisco”是最好的形式。
因此,处理此问题的正确方法如下:
containsPlacemark.locality?.replacingOccurrences(of: " ", with: "_")
尝试删除空格,所以San Francisco
成为SanFrancisco
。在http://openweathermap.org/它的作品。
今天我遇到了同样的问题,并且能够修复如下:
city = city.split(' ').join('+');