Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contextual closure type '() -> Void' expects 0 arguments, but 1 was used in closure body error show

Tags:

swift

   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
    })

Get this error

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

like image 999
Gowtham Sooryaraj Avatar asked Nov 01 '17 06:11

Gowtham Sooryaraj


2 Answers

The problem is here:

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

If you look at your method declaration, the completion handler is of type (() -> Void)!. It does not take any arguments. Your closure above takes one argument - finished. As a result, the error occurs.

You remove the finished argument from your closure:

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

Or you edit your animate method to accept a closure taking one argument:

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

    UIView.animate(
        withDuration: duration,
        delay: delay,
        options: options,
        animations: {
            animations()
        }, completion: { finished in
            completion?(finished)
    })
}
like image 157
Sweeper Avatar answered Nov 15 '22 08:11

Sweeper


1.) You misspelled completion

2.) remove the closure parameter finished in in you SPAnimation function

The reason why this doesn't work is that your created function has the closure type simply void. The static function contained in UIView has closure type ((Bool) -> Void)? therefore you have to put the parameter in there.

Either change the closure type in your animate function in SPAnimate or remove the finished argument in your closure call..

like image 30
Dominik Bucher Avatar answered Nov 15 '22 07:11

Dominik Bucher