Ios 在swift 4中的google Place picker中输入城市名称后未执行函数
我试了好几次,但没有任何线索。问题在于,当city第一次进入google place picker时,所有检索到的天气json数据都会打印到控制台上(这意味着当前天气函数和预测天气函数都会被调用)但是当第二次进入城市时,只调用当前天气数据函数,而forecast weather data函数不调用,并且tableView显示来自前一个城市的数据,因为forecast weather第二次未执行Ios 在swift 4中的google Place picker中输入城市名称后未执行函数,ios,function,swift4,Ios,Function,Swift4,我试了好几次,但没有任何线索。问题在于,当city第一次进入google place picker时,所有检索到的天气json数据都会打印到控制台上(这意味着当前天气函数和预测天气函数都会被调用)但是当第二次进入城市时,只调用当前天气数据函数,而forecast weather data函数不调用,并且tableView显示来自前一个城市的数据,因为forecast weather第二次未执行 // function below calls both functions when user en
// 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()
}
}
}
}
//下面的函数在用户进入城市时调用这两个函数
func userEnteredCity(城市:字符串,placeId:字符串)
{
let参数:[String:String]=[“q”:城市,“appid”:APP\u ID]
getWeather(weatherUrl:当前天气URL,参数:参数)
weatherForecast(weatherUrl:Forecast\u WEATHER\u URL,参数:参数)
loadFirstPhotoForPlace(placeID:placeID)
}
/*getWeather提供currentWeather和weatherForecast提供预测数据功能定义如下*/
func weatherForecast(weatherUrl:String,参数:[String:String])
{
请求(weatherUrl,方法:.get,参数:params).responseJSON
{
(答复)在
让result=response.result
如果let dictionary=result.value as?dictionary
{
如果let list=dictionary[“list”]as?[dictionary]
{
对于列表中的项目{
打印(项目)
let forecast=预报数据(weatherDict:项目)
self.forecastArray.append(预测)
}
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
}
}
谢谢你的回答。代码现在可以正常工作了。我必须在加载数据之前清除数组。