所以,我一直在使用以下简短的数据模型开发天气应用程序
class CurrentWeather
{
private var _cityName: String!
private var _date: String!
private var _weatherType: String!
private var _currentTemp: Double!
var cityName: String
{
if _cityName == nil
{
_cityName = ""
}
return _cityName
}
// Same idea for getters var date, var weatherType and
// var currentTemp (returns 0.0 if it is nil)
// Not showing that here
func downloadWeatherDetails(completed: DownloadComplete)
{
// Function which computes values though a url and stores in instance variables
// Not showing the entire actual function here
self._cityName = name.capitalized. // value computed earlier
print(self._cityName)
self._weatherType = main.capitalized // value computed earlier
print(self._weatherType)
self._currentTemp = currentTemp - 273.15 // value computed earlier
print(self._currentTemp)
completed()
}
}
其中类型DownloadComplete
是()->()
在主ViewController.swift中,我创建了一个对象并调用了这个函数(带有尾随闭包语法)
var currentWeather: CurrentWeather!
override func viewDidLoad()
{
super.viewDidLoad()
currentWeather = CurrentWeather()
currentWeather.downloadWeatherDetails {
self.updateMainUI() // I have created this function
}
}
func updateMainUI()
{
dateLabel.text = currentWeather.date
currentTempLabel.text = String(currentWeather.currentTemp)
locationLabel.text = currentWeather.cityName
currentWeatherTypeLabel.text = currentWeather.weatherType
currentWeatherImage.image = UIImage(named: currentWeather.weatherType)
print("Tested: \(currentWeather.currentTemp)")
print("Tested: \(currentWeather.cityName)")
print("Tested: \(currentWeather.weatherType)")
}
所以预期输出:
逻辑上,
我创建了一个CurrentWeather
对象
调用downloadWeatherDetails
函数,该函数应在私有变量中加载不同的计算值。
调用用户定义的updateMainUI
函数,该函数在我的应用的用户界面中显示不同的值
所以输出应该像
Birim. //cityname
Clear. //weatherType
29.134 //currentTemp
Tested: 29.134
Tested: Birim
Tested: Clear
但我得到的输出是
Tested: 0.0
Tested: (indicating "")
Tested: (indicating "")
Birim
Clear
29.134
那么,基本上函数downloadWeatherDetails
和updateMainUI
是以错误的顺序调用的?为什么会这样?这在某种程度上与函数的异步执行有关吗?
我试过不使用尾随闭包,但它仍然无效。
我也尝试将关闭设置为空并在updateMainUI
调用后调用downloadWeatherDetails
currentWeather.downloadWeatherDetails {
}
self.updateMainUI()
但这也不起作用。为什么以错误的顺序调用函数的任何想法?
更新
下划线变量是私有变量,而非下划线变量是像
这样的getter var cityName: String
{
if _cityName == nil
{
_cityName = ""
}
return _cityName
}
// Same idea for getters var date, var weatherType and
// var currentTemp (returns 0.0 if it is nil)
// Not showing that here
更新2:
项目文件在这里(如果有人想参考):https://github.com/danny311296/Weather-App
答案 0 :(得分:2)
您必须在Alamofire请求回调中调用'completed()'函数。由于请求函数是异步的,因此它不会在执行completed()之前等待它完成。
Alamofire.request(CURRENT_WEATHER_URL).responseJSON { response in
// handle response...
// when done call completed
completed()
}
答案 1 :(得分:0)
我想问题是你的" updateMainUI"在下载过程完成之前调用方法。虽然你已经实现了完成监听器,但这应该有效,但我不知道它有什么问题。尝试使用委托或通知等其他方法来观察下载过程。
检查此链接以查看观察完成的其他方法: