【发布时间】:2016-02-07 19:24:46
【问题描述】:
我是 Swift 新手,我正在尝试添加一个完成块。我记得这在objective-c中非常简单,但是我对这里的语法有点迷失了。此函数解析一些 json 并将相关内容添加到数组中。功能完成后,我需要刷新表格视图,因为我无法在需要添加完成块的块内执行此操作。
如何在 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
})
}
【问题讨论】:
标签: swift syntax objective-c-blocks