我是Swift新手,對回調/關閉有疑問。在異步網絡調用期間,不同的項目會在不同的時間出現。我想知道一個回調如何知道不會被執行,直到我們從請求中返回的所有內容都被返回?在異步調用完成後,知道如何等待回調/關閉?
編輯:
dispatch_async(dispatch_get_main_queue(), {
//Array to store HingeImage objects
var myImageArray = Array<HingeImage>()
for items in parsedResult {
//Create custom objects with values needed
let finalObjects = HingeImage(dictionary: items as! [String : AnyObject])
myImageArray.append(finalObjects)
}
// Callback completion
callback(hingeImages: hingeImageArray, error: nil)
})
編輯2:
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
callTheCallBackAndGrabData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func callTheCallBackAndGrabData() {
MainVC.requestWithCallback { (hingeImage, error) in
print(hingeImage)
}
}
}
它讀取程序員的頭腦,並做「正確的事情」™ – Avi
閉包不知道..你應該告訴他們什麼時候執行它...... –
可能重複[Swift,從異步塊的返回值](http:///stackoverflow.com/questions/26995757/swift-return-value-from-asynchronous-block) –