[英]Swift 3 set global variable value
So I'm trying to get some data when the view controller is loaded and set it so I can use it later in the code, but the value doesn't update.所以我试图在加载视图控制器时获取一些数据并设置它,以便我稍后可以在代码中使用它,但该值不会更新。 CountData
value should be 3
at the bottom of the code but it isn't代码底部的CountData
值应该是3
但它不是
import UIKit
import Alamofire
class FooController: UIViewController, UITableViewDataSource {
var CountData
override func viewDidLoad() {
super.viewDidLoad();
Alamofire.request("http://foo:8080/joke.php").responseJSON{ response in
if let JSON = response.result.value as? [String: String] {
}
//!!SET DATA HERE!!
self.CountData = 3
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//!!USE IT HERE!!
return CountData;
}
As Alamofire request is asynchronous you will need to call reloadData for your table.由于 Alamofire 请求是异步的,您需要为您的表调用 reloadData。
For now, you can initialize your现在,您可以初始化您的
var CountData = 3
and when you get the response set the CountData you want and then call当你得到响应时,设置你想要的 CountData 然后调用
self.CountData = 5
yourTableView.reloadData()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.