我是Swift的新手,我正在尝试添加一个完成块。我记得在objective-c中这很简单,但是我对这里的语法感到很失望。此函数解析一些json并将相关内容添加到数组中。我需要在函数完成后刷新tableview,因为我不能在块中执行此操作,我需要添加一个完成块。
如何在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
})
}
答案 0 :(得分:2)
使用handler
的参数更新您的函数。在这个例子中,我使用了一个闭包,它传递了一组缩略图,并且什么也没有返回:
func getSetParameter(handler:([Thumbnail]) -> ()) {
// generate an array of Thumbnails
let someArray = [Thumbnail(), Thumbnail()]
// call the handler, passing the array
handler(someArray)
}
由于最后一个参数是一个闭包,你可以使用Swift的尾随闭包语法:
// call the function
getSetParameter { thumbnails in
print(thumbnails)
}
或者更简洁:
getSetParameter {
print($0)
}
这个更详细的版本(没有尾随闭包语法)也有效:
getSetParameter({ (thumbnails) -> () in
print(thumbnails)
})
但在我看来,这更加丑陋。
答案 1 :(得分:-2)
不确定如何使用带有处理程序的闭包,self.getSetParameter函数括号中的代码在函数本身之前被调用。而不是在完成功能。
override func viewDidLoad()
{
self.getSetParameter { (thumbnails: [Thumbnail?]) -> () in
for var index = 0; index < thumbnails.count; ++index {
let currentThumbnail = thumbnails[index]
var title: String
var image: UIImage
if currentThumbnail!.isKindOfClass(Thumbnail) {
title = "Title " + String(index)
image = currentThumbnail!.image
}
else {
title = "Nil"
image = UIImage(named: "image0")!
}
self.models.append(CustomCell(title: title, image: image))
}
}
}
func getSetParameter(handler:([Thumbnail?]) -> ()) {
var thumbnails = [Thumbnail?]()
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 {
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)
thumbnails.append(newThumbnail)
}
handler(thumbnails)
}
}, failure: {(task: NSURLSessionDataTask?, error: NSError!) in
})
}