UIView.animateWithDuration完成

c2p*_*ing 3 xcode uiviewanimation ios swift

我有一个问题,关于在标题提到的方法中的迅速实施.如果我这样做:

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

我遇到以下问题:在调用中缺少参数'delay'的参数.只有在完成部分中有self.navigationController.returnToRootViewController()时才会发生这种情况.如果我将该语句提取为一个单独的方法,如下所示:

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

func returnToRootViewController() {
    navigationController.popToRootViewControllerAnimated(true)
}
Run Code Online (Sandbox Code Playgroud)

然后它完美地工作,完全符合我的要求.当然,这似乎不是理想的解决方案,更像是一种解决方案.任何人都可以告诉我我做错了什么或为什么Xcode(beta 6)这样做?

ric*_*ter 10

我认为你的意思popToRootViewControllerAnimated是你的第一个片段,因为returnToRootViewController它不是一个方法UUNavigationController.

您的问题是popToRootViewControllerAnimated具有返回值(从导航堆栈中删除的视图控制器数组).即使您尝试丢弃返回值,这也会导致麻烦.

当Swift看到一个带有返回值的函数/方法调用作为闭包的最后一行时,它假定您使用闭包简写语法来获取隐式返回值.(允许你编写类似的东西的那种someStrings.map({ $0.uppercaseString }).)然后,因为你有一个闭包在一个你希望传递一个返回void的闭包的地方返回一些东西,所以方法调用无法进行类型检查.类型检查错误往往会产生错误的诊断消息 - 我确信如果你提交了一个包含你所拥有的代码的错误以及它产生的错误消息,它会有所帮助.

无论如何,你可以通过使闭包的最后一行不是带有值的表达式来解决这个问题.我赞成一个明确的return:

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

您也可以将该popToRootViewControllerAnimated调用分配给未使用的变量,或者将表达式放在其后不执行任何操作,但我认为该return语句最清楚.