Jake
Jake

Reputation: 13753

Swift syntax error?

Why am I getting an [AnyObject]! is not convertible to 'Void' error here? Am I just not seeing some little syntax error or something??

    var popClosure = {()->Void in presentingController!.popToViewController(dest!, animated: false)}

It is only getting called once inside this method:

src!.dismissViewControllerAnimated(true, completion: popClosure)

Here's the full code:

class ColorfulUnwindSegue : UIStoryboardSegue
{
private let ANIMATION_DURATION = 0.5

override func perform()
{
    var dest = self.destinationViewController as? UIViewController
    var src = self.sourceViewController as? UIViewController

    if src && dest
    {
        dest!.view.frame = CGRectMake(-dest!.view.frame.size.width, 0, dest!.view.frame.size.width, dest!.view.frame.size.height)

        var srcIsModal = false

        var presentingController = src!.navigationController as? ColorfulNavigationController

        if src!.presentingViewController as? ColorfulNavigationController
        {
            srcIsModal = true
            presentingController = src!.presentingViewController as? ColorfulNavigationController
        }

        if presentingController
        {
            if srcIsModal
            {
                var popClosure : ()->Void = {()->Void in presentingController!.popToViewController(dest!, animated: false)}

                src!.dismissViewControllerAnimated(true, completion: popClosure)
            }
            else
            {
                src!.view.layer.addSublayer(dest!.view.layer)

                UIView.animateWithDuration(ANIMATION_DURATION,
                    animations:
                    {()->Void in
                        src!.view.frame = CGRectMake(dest!.view.frame.size.width, 0, src!.view.frame.size.width, src!.view.frame.size.height)
                    },
                    completion:
                    {(finished:Bool)->Void in
                        if finished
                        {
                            presentingController!.popToViewController(dest!, animated: false)
                        }
                    })
            }
        }
    }
}
}

Upvotes: 1

Views: 623

Answers (1)

Connor
Connor

Reputation: 64654

It seems like since you only have one method call in your closure it's trying to store that method in popClosure.

 func popToViewController(viewController: UIViewController!, animated: Bool) -> [AnyObject]!

Because that one method returns [AnyObject]!, popClosure returns [AnyObject]! which doesn't match up with its declared type () -> Void.

You can workaround this by just adding another line to your closure. It doesn't have to do anything. As rickster suggested, in this example I simply add a return to the closure.

var popClosure: () -> Void = { () -> Void in
    presentingController!.popToViewController(dest!, animated: false)
    return
}

The compiler then seems to understand you are defining a closure in which that method is called.

Upvotes: 2

Related Questions