无法在 Swift 3 中使用闭包语法
Not able to use closure syntax in Swift 3
我正在尝试将闭包 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
完成块也面临问题。
我可以在没有完成处理程序的情况下使用 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
}
我正在尝试将闭包 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
完成块也面临问题。
我可以在没有完成处理程序的情况下使用 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
}