gpt4 book ai didi

ios - 使用 Openweathermap 获取名称中包含空格的城市的天气

转载 作者:搜寻专家 更新时间:2023-11-01 05:50:17 26 4
gpt4 key购买 nike

我正在尝试使用 Openweathermap 获取城市的天气信息使用此代码:

func getWeatherByCity(city: String) {
if let weatherRequestURL = NSURL(string: "\(openWeatherMapBaseURL)?APPID=\(openWeatherMapAPIKey)&q=\(city)") {
getWeather(weatherRequestURL: weatherRequestURL)
}
}

(完整教程 http://www.globalnerdy.com/2016/04/02/how-to-build-an-ios-weather-app-in-swift-part-1-a-very-bare-bones-weather-app/ )

该 API 适用于不包含空格的城市名称。

连上首页http://openweathermap.org/ , 寻找 san francisco 没有结果。

这里缺少什么?

最佳答案

您需要用+ 符号替换城市名称中的空格

let string = "San Francisco"
let replaced = (string as NSString).stringByReplacingOccurrencesOfString(" ", withString: "+")

enter image description here如果您查看地址栏 - 它会用加号本身替换空格。

对于这个站点,我们需要删除空格: enter image description here

不知道为什么他们声称他们的搜索引擎非常灵活 :)

关于ios - 使用 Openweathermap 获取名称中包含空格的城市的天气,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38872376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com