我是新來的Swift,我試圖添加一個完成塊。我記得在Objective-C中這很簡單,但我有點在這裏的語法丟失。該函數解析一些json並將相關內容添加到數組中。一旦函數完成,我需要刷新一個tableview,因爲我不能在塊內完成這個工作,我需要添加一個完成塊。在Swift中添加完成塊功能
如何在Swift中爲此函數添加完成塊,以及新的方法調用將如何?
func getSetParameter()
{
let param = ["format":"json"]
let jsonUrl: String! = "http://somewebsite.com"
let manager: AFHTTPSessionManager = AFHTTPSessionManager()
manager.GET(jsonUrl, parameters: param, success: {
(task: NSURLSessionDataTask!, JSONResponse: AnyObject!) in
let responseDictionary = JSONResponse as! NSDictionary
let responseArray = responseDictionary.objectForKey("response") as! NSArray
for thumbnailsOnVideoDictionary in responseArray
{
let thumbnailsOnVideoArray = thumbnailsOnVideoDictionary.objectForKey("thumbnails") as! NSArray
if thumbnailsOnVideoArray.count == 0 {
self.thumbnails.append(nil)
}
else {
let smallThumbnail = thumbnailsOnVideoArray[1];
let aspect_ratio: Float = (smallThumbnail.objectForKey("aspect_ratio") as! Float)
let height: UInt = (smallThumbnail.objectForKey("height") as! UInt)
let name: AnyObject = smallThumbnail.objectForKey("name")!
let url: String = (smallThumbnail.objectForKey("url") as! String)
let width: UInt = (smallThumbnail.objectForKey("width") as! UInt)
let newThumbnail = Thumbnail(aspect_ratio: aspect_ratio, height: height, name: name, url: url, width: width)
self.thumbnails.append(newThumbnail)
}
}
}, failure: {(task: NSURLSessionDataTask?, error: NSError!) in
})
}
真的只是一樣http://stackoverflow.com/questions/30133490/run-code-only-after-asynchronous-function-finishes-executing – matt