我有一个应该做两件事的方法。它应该创建一个图片对象,并返回一个在本地下载图片时调用的闭包。以下代码片段尝试绘制我正在尝试的内容。
typealias localURLForDownloadedImage = () -> (NSURL)
func pictureForDate(#date: NSDate) -> (picture: Picture?,downloadedImageURL: urlForDownloadedImage?) {
// start downloading picture from a url
let picture = Picture()
// start downloading url
let closure = urlForDownloadedImage(){
// async method that returns when image is downloaded
return NSURL(string: "downloadedimageurl://")
}
return (picture,closure)
}
我得到的关闭属性的错误是“urlForDownloadedImage与() - >() - > $ TO”
不兼容由于Xcode 6s自动完成功能仍然存在,您如何调用此方法? 我想的有点像这样:
pictureForDate(date: NSDate()) {
// do stuff here after image was downloaded
}
但这只是一个奇怪的错误。
答案 0 :(得分:1)
关闭应该是
let closure = { () -> NSURL in
// async method that returns when image is downloaded
return NSURL(string: "downloadedimageurl://")
}
要调用方法,请使用:
let (pic, closure) = pictureForDate(date: NSDate())
现在在pic
和closure
中,您有2个返回值。在使用之前必须打开封口:
closure?()