[weak self]给出了以下代码的错误:Expected','Separator

时间:2016-09-19 09:47:45

标签: ios swift3

fileprivate func hideViewWithAnimation() {

    UIView.animate(withDuration: 0.3, animations: { [weak self]  
        if self == nil {
            return
        }

        self!.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)

        self!.constraintContainerViewBottom.constant = -Constants.screenHeight()
        self!.constraintContainerViewTop.constant = Constants.screenHeight()
        self!.view.layoutIfNeeded()

    }, completion: { (isCompleted) in
        self.navigationController?.dismiss(animated: false, completion: nil)
    }) 
}

显示[弱自我]要求使用','分隔它时出错。我做错了什么

2 个答案:

答案 0 :(得分:1)

正如Tj3n所说,当您使用[weak self]语法时,您需要in关键字,例如

UIView.animate(withDuration: 0.3) { [weak self] in
    self?.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)

    ...
}

但是你根本不需要动画块中的[weak self],因为动画块在动画期间不会对self保持强引用。没有强大的参考周期可以打破。所以我建议完全删除[weak self]

并且,如果你想知道,你也不需要担心completion块中的强引用,因为当视图被取消时,正在进行的动画被取消并且{{1}使用completion立即调用块作为布尔参数。

答案 1 :(得分:-3)

private func hideViewWithAnimation() {

    weak var weakSelf = self

    if weakSelf != nil {
        UIView.animateWithDuration(0.3, animations: {

            self!.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)

            self!.constraintContainerViewBottom.constant = -Constants.screenHeight()
            self!.constraintContainerViewTop.constant = Constants.screenHeight()
            self!.view.layoutIfNeeded()

        }) { (isCompleted) in
            self.navigationController?.dismiss(animated: false, completion: nil)

        }

    }
}