UIView.animateWithDuration completion

I have a question regarding the quick implementation of the method mentioned in the title. If I do this:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.returnToRootViewController(true)
})

      

I am getting the following problem: Missing argument for 'delay' parameter in the call. This only happens if I have self.navigationController.returnToRootViewController () in the completion part. If I extract this statement into a separate method like this:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.returnToRootViewController()
})

func returnToRootViewController() {
    navigationController.popToRootViewControllerAnimated(true)
}

      

Then it works fine and does exactly what I want. Of course this doesn't seem like a perfect solution and looks more like a workaround. Can someone tell me what I did wrong or why Xcode (beta 6) behaves this way?

+3


source to share


1 answer


I assume that you mean popToRootViewControllerAnimated

in your first snippet since it is returnToRootViewController

not a method on UUNavigationController

.

Your problem is what popToRootViewControllerAnimated

has a return value (array of view dispatchers has been removed from the navigation stack). This causes problems even if you are trying to discard the return value.

When Swift sees a function / method call with a return value as the last line of the closure, it is assumed that you are using closure shorthand syntax for implicit return values. (The kind that lets you write things like someStrings.map({ $0.uppercaseString })

.) Then, since you have a closure that returns something where you expect to pass a closure that returns void, the method call cannot type-check. Type checking errors tend to lead to bad diagnostic messages - I'm sure this will help if you filed a bug with the code you have and the error message it produces.



Anyway, you can get around this by making the last closing line not a value expression. I use explicit return

:

UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.popToRootViewControllerAnimated(true)
    return
})

      

You can also assign the call to an popToRootViewControllerAnimated

unused variable, or place an expression that does nothing after it, but I think the statement return

is the clearest.

+10


source







All Articles