ios - 无法在 Swift 3 中使用闭包语法

标签 ios swift swift3 xcode8-beta4

我正在尝试将闭包 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:)'

enter image description here

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/

相关文章:

ios - 分享到安装在手机 iOS Swift 上的社交媒体

ios - Swift 3 转换器添加了 fileprivate 函数

ios - 托管解析是否支持推送通知?

ios - 如何增加 UILabel 中的字符间距

swift - 如何使用 SceneKit 涡流场创建 Tornado 效果

iOS 在按下时禁用动画

ios - 如何使 subview 的一部分从 super View 突出时不可见?

ios - NSEntityDescription 还存在于内存中吗?

swift - 我应该如何处理参数验证 Swift

swift - 如何在 StackView 中的两个 View 之间设置动画?