簡體   English   中英

[弱自我]給出了以下代碼的錯誤:預期的','分隔符

[英]The [weak self] is giving an error for the following code: Expected ',' Separator

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

對於[弱自我]要求使用','將其分開時顯示錯誤。 我究竟做錯了什么

正如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塊中的強引用,因為當關閉視圖時,動畫會被取消,並且會立即為布爾參數使用false來調用completion塊。 。

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)

        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM