【问题标题】:Not able to use closure syntax in Swift 3无法在 Swift 3 中使用闭包语法
【发布时间】:2016-08-15 06:51:30
【问题描述】:

我正在尝试将闭包 Xcode8 beta4 与 Swift3 一起使用,但它不起作用。与闭包相同的函数在 Swift2.2 中工作,但在 Swift3 中失败。

斯威夫特 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"))

但是当与完成处理程序一起使用时,编译器会报错给定消息。

'sd_setImage(with:placeholderImage:completed:)'的模糊使用

imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "imageName"), completed: { (image, error , cacheType , imageURL) in

        })

看起来,闭包语法会发生变化,但是如何发现出了什么问题?

【问题讨论】:

    标签: ios swift swift3 xcode8-beta4


    【解决方案1】:

    Swift 3 中的 UIView 动画语法更改为:

    UIView.animate(withDuration: 0.5, animations: {
    
    }, completion: { (Bool) in
    
    })
    

    致电greetingMessageWithDate:

    greetingMessageWithDate(date: Date(), message: "") { (greetingMessage: String, code: Int) in
    
    }
    

    【讨论】:

    • 你能给我SDWebImage的解决方案吗?
    • 我猜它现在不兼容 Swift 3。常规的 Swift 3 语法不是开箱即用的。还有一个关于 Swift 3 支持的未解决问题:github.com/rs/SDWebImage/issues/1627
    • 您能否将答案标记为 Swift 3 中闭包的一般用法正确?
    • 我现在接受答案,但期待SDWebImage 的更新。谢谢
    • 关于 SDWebImage 请参阅此答案以获取解决方法和详细说明:stackoverflow.com/a/39024894/2099148
    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多