gpt4 book ai didi

ios - Swift POST 请求和事件指示器

转载 作者:行者123 更新时间:2023-11-30 11:50:03 25 4
gpt4 key购买 nike

您好,我正在使用 SwiftyJSON 库从服务器获取数据。我的代码如下:

    let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
let message2 = "Please wait..."
var messageMutableString = NSMutableAttributedString()
messageMutableString = NSMutableAttributedString(string: message2 as String, attributes: [NSFontAttributeName:UIFont(name: "HelveticaNeue-Bold",size: 15.0)!])
alert.setValue(messageMutableString, forKey: "attributedMessage")
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();



alert.view.addSubview(loadingIndicator)
present(alert, animated: true, completion: nil)

var request = URLRequest(url: URL(string: Global.ipAdres)!)
request.httpMethod = "POST"
let postString = Global.postString
request.httpBody = postString.data(using: .utf8)


let task = URLSession.shared.dataTask(with: request)
{ data, response, error in


DispatchQueue.main.async {
self.printNameSurname()
self.dismiss(animated: true, completion: nil)
}
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return;
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
{
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = JSON(data)

GlobalVariable.nameSurname = json["data"]["nameSurname"].stringValue + json["data"]["sad"].stringValue
GlobalVariable.id = json["data"]["id"].stringValue
GlobalVariable.id2 = json["data"]["id2"].stringValue

}
task.resume()

如果我没有理解错误,我的 printNameSurname() 函数需要执行并填充 View Controller 中的两个标签,当我获得值时,我的事件指示器应该被忽略,这就是为什么我将它放在 Dispatch.main.async 中。但是,它们无法正常工作。我的标签未填充我获取的数据。我认为问题在于发布请求,但我搜索了它的示例,但它们对我不起作用,或者我做错了什么,我该如何修复它?

最佳答案

Just put all code in main queue after got response as below.

    let task = URLSession.shared.dataTask(with: request)
{ data, response, error in

//Trick here...
DispatchQueue.main.async {

self.printNameSurname()
self.dismiss(animated: true, completion: nil)
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return;
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200
{
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = JSON(data)

GlobalVariable.nameSurname = json["data"]["nameSurname"].stringValue + json["data"]["sad"].stringValue
GlobalVariable.id = json["data"]["id"].stringValue
GlobalVariable.id2 = json["data"]["id2"].stringValue
}

}

关于ios - Swift POST 请求和事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48427464/

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