我调用此函数我从alomafier请求得到json响应但是当我声明全局变量并将数据存储在变量得到的结果但是在响应循环下面打印变量时它变为空白或为零。
我使用词典数组但没有得到解决方案。
Alamofire.request(url,method: .post).responseJSON { response in
var data1 = response.result.value ---- I get response here but not get result outside of the loop
代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomTableViewCell_view
var domain:[[String:String]] = []
var asyncObj:Operations = Operations()
var url:String = asyncObj.getUrl("ticket", operation: "get_domain", filter: "")
Alamofire.request(url,method: .post).responseJSON { response in
var data1 = response.result.value
//let data1 = response.data
if(data1 != nil)
{
var json1 = JSON(data1!)
GlobalVar.myvar = 10
let count: Int? = json1.array?.count
var departmentList = [DEPARTMENT]()
for index in 0 ..< count! {
var domainid = json1[index]["domain_id"]
var domainname = json1[index]["domain_name"].stringValue
self.numbers.append(count!)
cell.nameLabel.text = "- "+domainname
var url:String = asyncObj.getUrl("ticket", operation: "get_department", filter: "vis_filter=avis_domain.domain_id=\(domainid)")
Alamofire.request(url,method: .post).responseJSON { response in
var data2 = response.result.value
var departmentList:[[String:String]] = []
if data2 != nil {
var json2 = JSON(data2!)
var count2: Int? = json2.array?.count
for index2 in 0 ..< count2!{
var departmentid = json2[index2]["department_id"]
var departmentname = json2[index2]["department_name"]
//departmentList.append(["departmentid": "\(departmentid)", "departmentname": "\(departmentname)"])
cell.addressLabel.text = "- "+"\(departmentname)";
var url:String = asyncObj.getUrl("ticket", operation: "get_department_status_list", filter: "vis_department=\(departmentid)")
Alamofire.request(url,method: .post).responseJSON { response in
var data3 = response.result.value
if data3 != nil {
var json3 = JSON(data3!)
var statusList:[[String:String]] = []
var count3: Int? = json3.array?.count
for index3 in 0 ..< count3!{
var statusid = json3[index3]["status_id"]
var statusname = json3[index3]["status_name"]
cell.status.text = "- "+"\(statusname)";
var url:String = asyncObj.getUrl("ticket", operation: "get_tickets", filter: "vis_filter=status_id=\(statusid)&\(VIS_PREFIX)ticket.department_id=\(departmentid)")
Alamofire.request(url,method: .post).responseJSON { response in
var data4 = response.result.value
var tcount: Int?
var ticketcount:Int = 0
if data4 != nil {
var json4 = JSON(data4!)
tcount = json4.array?.count
for index3 in 0 ..< tcount! {
ticketcount = ticketcount + 1
}
}
}
}
}
}
}
}
}
}
}
}
/*let hotelName = hotelNames[(indexPath as NSIndexPath).row]
cell.nameLabel.text = hotelName
cell.addressLabel.text = hotels[hotelName]
*/
return cell
}
答案 0 :(得分:0)
我不确定你在问什么,但听起来你希望全局变量能够立即更新。实际上,只有在响应进入后才会更新。
以下是一个例子:
var status = "Nothing much"
Alamofire.request("https://example.org/something").responseJSON { response in
// This code runs in the future, probably after a few seconds
status = "Got a response!"
}
// This code runs immediately
print(status) // Prints "Nothing much"
此处,状态(如全局变量)初始化为一个值。接下来,发送Alamofire请求,并带有最终更新状态变量的响应回调。但是,程序不会在继续之前等待响应。这就是下一行打印出“没什么”的原因。