swift – 上下文闭包类型'() – > Void’需要0个参数,但是在闭包体错误显示中使用了1

static func animate(_ duration: TimeInterval,animations: (() -> Void)!,delay: TimeInterval = 0,options: UIViewAnimationoptions = [],withComplection completion: (() -> Void)! = {}) {

        UIView.animate(
            withDuration: duration,delay: delay,options: options,animations: {
                animations()
            },completion: { finished in
                completion()
        })
    }

Using above class in my swift file and create function like below

SPAnimation.animate(durationScalingRootView,animations: {
                            rootViewController.view.transform = CGAffineTransform.identity
        },delay: delayScalingRootView,options: UIViewAnimationoptions.curveEaSEOut,withComplection: {
                            finished in
                            //rootViewController.view.layer.mask = nil
    })

得到此错误

Contextual closure type ‘() -> Void’ expects 0 arguments,but 1 was
used in closure body

解决方法

问题出在这里

withComplection: {
    finished in
    //rootViewController.view.layer.mask = nil
}

如果查看方法声明,则完成处理程序的类型为(() – > Void)!.它不需要任何论据.上面的闭包需要一个参数 – 完成.结果,发生错误.

从闭包中删除完成的参数:

withComplection: {
    //rootViewController.view.layer.mask = nil
}

或者您编辑animate方法以接受带有一个参数的闭包:

static func animate(_ duration: TimeInterval,withComplection completion: ((Bool) -> Void)? = nil) {

    UIView.animate(
        withDuration: duration,animations: {
            animations()
        },completion: { finished in
            completion?(finished)
    })
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...