Ios 为什么全局变量不更新swift 3中的值?

Ios 为什么全局变量不更新swift 3中的值?,ios,swift,global-variables,Ios,Swift,Global Variables,这是值所在的类 class CurrentWeather{ var _date:String! var _cityName:String! var _temp:Double! var _weatherType:String! var cityName:String{ if _cityName==nil{ _cityName = "" } return _cityName

这是值所在的类

class CurrentWeather{

    var _date:String!
    var _cityName:String!
    var _temp:Double!
    var _weatherType:String!


    var cityName:String{
        if _cityName==nil{
            _cityName = ""
        }

        return _cityName
    }
    var currentTemprature:Double{

        if _temp==nil{
            _temp = 0.0
        }

        return self._temp
    }
    var weathertype:String{
        if _weatherType==nil{
            _weatherType =  ""
        }
        return _weatherType
    }
    var date:String{

        if _date==nil{
            _date = ""
        }
     let dateFormater=DateFormatter()
        dateFormater.dateStyle = .long
        dateFormater.timeStyle = .none
        let currentDate = dateFormater.string(from: Date())
        self._date="\(currentDate)"
        return _date
     }


       func weatherDataDownload(completed : downloadComplete){
    let weatherUrl=URL(string: constant)!
    Alamofire.request(weatherUrl , method:.get).responseJSON{response in

        if let dict=response.result.value as? Dictionary<String,AnyObject>{

            if let name=dict["name"] as? String{
                self._cityName = name.capitalized
                print(name.capitalized)
            }
            if let weather=dict["weather"] as? [Dictionary<String,AnyObject>]{
                if let main=weather[0]["main"] as? String{
                    self._weatherType=main.capitalized
                    print(main.capitalized)
                }

            }
            if let tempr=dict["main"] as? Dictionary<String,AnyObject>{
                if let temp=tempr["temp"] as? Double{
                   let convertedTemp=Double(round(temp-273.15))
                    self._temp=convertedTemp
                    print(convertedTemp)
                }
            }

        }

    }
    completed()
}}

当我尝试调用
ViewController
时,它会显示我在计算变量中设置的默认值,而不是
\u date
,但我可以在
weatherDataDownload
func
中打印值。因此,我对swift 3中的变量如何工作感到困惑。

请参见下面的注释代码示例。您需要将调用移动到“completed()”

当您对Alamofire进行all时,它会在后台线程上执行其请求。当该请求完成时,它调用您定义的闭包(启动“response in…”的闭包)。您不想调用
updateUIweather
,直到调用完成,所以您将调用放在同一个完成处理程序中的“completed()”

当对
completed
的调用在该完成处理程序之外时,将立即调用它。。。在Alamofire请求发送之后(但在后台线程上完成之前)。完成处理程序中的所有代码都尚未运行,因此变量尚未更新


最后,因为您的
已完成的
闭包被传递给一个块,然后该块被发送到后台线程,所以该闭包“转义”当前函数。您添加@escaping,以便阅读您的代码的人知道闭包将在该函数的生命周期之外继续存在。

您正在闭包之外调用
completed
,因此在下载完成之前调用它。它应该在哪里?。它在
weatherDataDownload
func
中,应该是
responseJSON
closure中的最后一行。如果我这样做,我会得到错误,因为
closure使用非转义参数“completed”可能允许它转义
Yes,您需要将
completed
参数声明为
@escaping
在主要问题中添加了@escaping per comments我添加了一些解释。
var currentWeatherOj = CurrentWeather()


    override func viewDidLoad() {
        super.viewDidLoad()
        table.delegate=self
        table.dataSource=self
        currentWeatherOj.weatherDataDownload {
            self.updateUIweather()
        }


    }
    func updateUIweather () {
        weatherType.text=currentWeatherOj.weathertype
        presentDate.text=currentWeatherOj.date
        presentLocation.text=currentWeatherOj.cityName
        presentTemp.text="\(currentWeatherOj.currentTemprature)"

    }
func weatherDataDownload(@escaping completed : downloadComplete) {
    let weatherUrl=URL(string: constant)!
    Alamofire.request(weatherUrl , method:.get).responseJSON { response in
        // ... leave your code here alone

        // put the call to completed() here
        completed()
    }
    // not here
}