Ios [weak self]为以下代码提供错误:预期为“,”分隔符

Ios [weak self]为以下代码提供错误:预期为“,”分隔符,ios,swift3,Ios,Swift3,[弱自我]请求使用“,”将其分隔时出现错误。我做错了什么正如Tj3n所说,当你使用[weak self]语法时,你需要in关键字,例如 fileprivate func hideViewWithAnimation() { UIView.animate(withDuration: 0.3, animations: { [weak self] if self == nil { return } self!.vie

[弱自我]请求使用“,”将其分隔时出现错误。我做错了什么

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

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)
    }) 
}
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)

        }

    }
}
但是,在动画块中根本不需要[weak self],因为动画块在动画持续时间内不保留对self的强引用。没有强有力的参考循环可以打破。因此,我建议完全消除[脆弱的自我]


而且,如果您想知道,您也不需要担心完成块中的强引用,因为当视图被取消时,正在进行的动画被取消,并且完成块会立即被布尔参数调用为false。

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

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)

        }

    }
}
但是,在动画块中根本不需要[weak self],因为动画块在动画持续时间内不保留对self的强引用。没有强有力的参考循环可以打破。因此,我建议完全消除[脆弱的自我]


而且,如果您想知道,您也不必担心完成块中的强引用,因为当视图被取消时,正在进行的动画将被取消,并且完成块将立即调用布尔参数false。

[weak self]in,您没有in[weak self]in,您没有inThanks。后来我发现我们不需要[弱自我]来制作动画块。我后来发现,我们不需要[弱自我]来制作动画块