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

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

显示 [weak self] 要求使用 ',' 分隔的错误。我做错了什么

最佳答案

正如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)

    ...
}

但是你根本不需要在动画 block 中使用[weak self],因为动画 block 在动画片。没有强引用循环可以打破。所以我建议完全删除 [weak self]

而且,如果您想知道,您也不必担心 completion block 中的强引用,因为当 View 被关闭时,正在进行的动画被取消并且 completion block 被立即调用, bool 参数为 false

关于ios - [weak self] 给出以下代码 : Expected ',' Separator 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39570092/

相关文章:

ios - 如何在 swift 3 中获取和更新 Healthkit 中的高度?

ios - 是否可以让应用程序在照片中查找 "certain features"

ios - 滚动表格时按钮文本消失

ios - 手机锁定时 Corebluetooth 无法正常工作

swift - 在 Swift 3 (Xcode 8.3) 上替代 AnyForwardCollection

ios - 事件指示器未在 TableView 中居中

ios - React 原生链接弹出

ios - UICollectionView - 多个不同的单元格

xcode - 圆形图像变成钻石而不是圆形 - Swift 3

ios - CollectionView 方法 'referenceSizeForHeaderInSection' 引发异常