gpt4 book ai didi

ios - Swift:函数调用顺序错误?

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:46 24 4
gpt4 key购买 nike

因此,我一直在使用以下简要数据模型开发天气应用

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)")
}

所以预期输出:

逻辑上,

  1. 我创建了一个 CurrentWeather 对象

  2. 调用了 downloadWeatherDetails 函数,它应该在私有(private)变量中加载不同的计算值。

  3. 调用用户定义的 updateMainUI 函数,该函数在我的应用的 UI 上显示不同的值

所以输出应该是这样的

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

那么,基本上 downloadWeatherDetailsupdateMainUI 函数的调用顺序是错误的?为什么会这样?这在某种程度上与函数的异步执行有关吗?

我试过不使用尾随闭包,但还是不行。

我还尝试将闭包留空并在 downloadWeatherDetails 调用之后调用 updateMainUI ,如下所示

currentWeather.downloadWeatherDetails {

}
self.updateMainUI()

但这也行不通。为什么以错误的顺序调用函数有任何想法吗?

更新:

下划线变量是私有(private)变量而非下划线变量是getters like

    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

最佳答案

您必须在 Alamofire 请求回调中调用“completed()”函数。由于请求函数是异步的,因此在执行 completed() 之前不会等待它完成。

Alamofire.request(CURRENT_WEATHER_URL).responseJSON { response in

// handle response...

// when done call completed
completed()
}

关于ios - Swift:函数调用顺序错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574580/

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