我试图弄清楚这个完成处理程序,但是我无法理解。我有这个程序,我试图下载一个json文件并解析它。一切正常,但是我希望用户界面在文件下载和解析后进行更新。我必须按两次按钮才能使其正常工作。我知道为什么会这样。该代码是异步样式,只是不进行任何更新即可更新UI。我如何获取它以等待json完成解析和下载。感谢您的帮助和提示
这是我到目前为止的内容:
@IBAction func testerClick(sender: AnyObject) {
print("Button pushed")
print("Downloading the json file")
downlaodPromoData(promoUrl, myUser: myUserName, myPass: myPassword)
}
func parsePromoJson(json : String)
{
//parse the json file
if let data = downloadJson.dataUsingEncoding(NSUTF8StringEncoding) {
let json = JSON(data: data)
for item in json[].arrayValue {
pictures.append(item["picture"].stringValue)
}
for item in json[].arrayValue{
path.append(item["path"].stringValue)
}
for item in json[].arrayValue{
label.append(item["label"].stringValue)
}
}
// Loop over the array with a for-loop.
for i in 0 ..< pictures.count {
uiResultsTextField.text.appendContentsOf("Picture: " + pictures[i] + "\n" + "Path: " + path[i] + "\n" + "Label: " + label[i] + "\n")
}
}
func downlaodPromoData(myUrl : String, myUser : String, myPass : String)
{
Alamofire.request(.GET, myUrl)
.authenticate(user: myUser, password: myPass)
.validate(statusCode: 200..<300)
.responseString { response in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
self.downloadJson = response.result.value!
/*
.response {(request, response, data, error) in
print(response)
print(data)
print(request)
*/
}.responseJSON { response in
print("Response JSON: \(response.result.value)")
}
print("Calling parser")
parsePromoJson(downloadJson)
}
您在请求后立即调用parsePromoJson。将parsePromoJson调用移至Alamofire请求的responseString块内。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句