简体   繁体   中英

UIView.animateWithDuration completion

I have a question concerning the swift 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 get the following problem: Missing argument for parameter 'delay' in call. This only happens if I have the self.navigationController.returnToRootViewController() in the completion part. If I extract that statement into a seperate 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 perfectly and does exactly what I want. Of course this does not seem to be the ideal solution and more like a work around. Can anyone tell me what I did wrong or why Xcode (beta 6) is behaving this way?

I presume you mean popToRootViewControllerAnimated in your first snippet, since returnToRootViewController isn't a method on UUNavigationController .

Your problem is that popToRootViewControllerAnimated has a return value (the array of view controllers removed from the navigation stack). This causes trouble even though you're trying to discard the return value.

When Swift sees a function/method call with a return value as the last line of a closure, it assumes you're using the closure shorthand syntax for implicit return values. (The kind that lets you write things like someStrings.map({ $0.uppercaseString }) .) Then, because you have a closure that returns something in a place where you're expected to pass a closure that returns void, the method call fails to type-check. Type checking errors tend to produce bad diagnostic messages — I'm sure it'd help if you filed a bug with the code you have and the error message it's producing.

Anyhow, you can work around this by making the last line of the closure not be an expression with a value. I favor an explicit return :

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

You can also assign that popToRootViewControllerAnimated call to an unused variable or put an expression that does nothing after it, but I think the return statement is clearest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM