I'm new to Swift and I'm trying to add a completion block. I remember this being pretty simple in objective-c, but I'm kinda lost with the syntax here. This function parses some json and adds the relevant content to an array. I need to refresh a tableview once the function is complete, since I can't do this within the block I need to add a completion block.
How do I do add a completion block to this function in Swift, and how would the new method call look like?
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
})
}