私はこのコンプリートハンドラーを理解しようとしていますが、私はちょうどそれを取り巻くことはできません。私はこのプログラムをjsonファイルをダウンロードして解析しようとしています。それはすべて正常に動作しますが、私は、ファイルがダウンロードされ、解析された後にUIを更新したい。私はそれを働かせるためにボタンを2回押しなければならない。私はなぜこれが起こっているのか知っています。コードは非同期スタイルで動作しており、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)
}
'.responseString'コールバックの' self.downloadJson = response.result.value! '行の直後に 'parsePromoJson()'の呼び出しを移動してみてください。 – pbodsk
ご提案いただきありがとうございます。あなたの助けをありがとう – MNM