在 swift 4 中的谷歌位置选择器中输入城市名称后未执行函数



我已经尝试了几次,但没有线索。问题是当城市第一次进入谷歌地方选择器时,整个检索到的天气json数据都会打印到控制台上(这意味着当前天气函数和预测天气函数都被调用(,但是当第二次输入城市时,只有当前天气数据函数被调用,但预测天气数据函数没有,tableView显示自天气预报以来以前城市的数据没有第二次执行。

// function below calls both functions when user enters city
func userEnteredCity(city: String, placeId: String)
{
let parameters :[String:String] = ["q": city, "appid": APP_ID]
getWeather(weatherUrl: Current_WEATHER_URL, params: parameters)
weatherForecast(weatherUrl: Forecast_WEATHER_URL, params: parameters)
loadFirstPhotoForPlace(placeID: placeId)
}
/* getWeather provides currentWeather and weatherForecast provide forecast data functions have been defined below*/
func weatherForecast(weatherUrl: String, params: [String:String])
{
Alamofire.request(weatherUrl, method: .get, parameters: params).responseJSON
{
(response) in
let result = response.result
if let dictionary = result.value as? Dictionary<String, AnyObject>
{
if let list = dictionary["list"] as? [Dictionary<String, AnyObject>]
{
for item in list {
print(item)
let forecast = ForecastData(weatherDict: item)
self.forecastArray.append(forecast)
}
self.weatherTable.reloadData()
}

}
}
}

请在这个问题上帮助我。

首先,是否正在执行print语句,它是否打印了新城市的预测数据?

如果是这样,请检查您的forecastArray打算提供什么。目前,您似乎只向现有数组添加新的预测数据,例如,它永远不会被清除。您应该在获取数据之前这样做:

func weatherForecast(weatherUrl: String, params: [String:String]) {
self.forecastArray.removeAll()
Alamofire.request(weatherUrl, method: .get, parameters: params).responseJSON {
// ... same as before
}
}

最新更新