我正在尝试将闭包 Xcode8 beta4 与 Swift3 一起使用,但它不起作用。与闭包在 Swift2.2 中工作的相同功能,但在 Swift3 中失败。
swift 2.2
UIView.animateWithDuration(0.5, animations: {
self.viewForInstagramLoginWebView.frame = CGRectMake(x, y,self.viewForInstagramLoginWebView.frame.size.width , self.viewForInstagramLoginWebView.frame.size.height)
}) { (finished) in
SVProgressHUD.dismiss()
}
但相同的语法不适用于 Swift3。
也可以尝试创建带闭包的函数。
func greetingMessageWithDate(date : NSDate, message :String, successHandler : (greetingMessage : String, code : Int ) -> Void){
}
相同的功能在 Swift 2.2 中有效,但在 Swift 3 中无效
还面临 SDWebImage
完成 block 的问题。
我可以在没有完成处理程序的情况下使用 SDWebImage
,但是使用完成处理程序它会失败。
在没有完成处理程序的情况下工作。
imageView.sd_setImage(with: URL(string: "imageURL"), placeholderImage: UIImage(named : "imageName"))
但是当与完成处理程序一起使用时,编译器会提示给定的消息。
Ambiguous use of 'sd_setImage(with:placeholderImage:completed:)'
imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "imageName"), completed: { (image, error , cacheType , imageURL) in
})
看起来,更改将在闭包语法中进行,但是如何发现哪里出了问题?
最佳答案
UIView 动画语法在 Swift 3 中更改为:
UIView.animate(withDuration: 0.5, animations: {
}, completion: { (Bool) in
})
调用greetingMessageWithDate
:
greetingMessageWithDate(date: Date(), message: "") { (greetingMessage: String, code: Int) in
}
关于ios - 无法在 Swift 3 中使用闭包语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38950520/