嘿自从a移到Swift 3之后,我得到了一个错误,它说我的SCNTransaction扩展中的completionBlock是let常量,我不知道如何将它改为“var”有什么帮助吗?
Picture of Error here
代码:

extension SCNTransaction {
   class func animateWithDuration(_ duration: CFTimeInterval = 0.25, timingFunction: CAMediaTimingFunction? = nil, completionBlock: (() -> Void)? = nil, animations: () -> Void) {
     begin()
     animationDuration = duration
     completionBlock = completionBlock
     animationTimingFunction = timingFunction
     animations()
     commit()
   }
}

最佳答案

我认为这个问题与类的实例变量completionBlock与函数的参数SCNTransaction同名有关。因此,代码行completionBlock被解释为参数completionBlock = completionBlock值的赋值,而不是同名的实例变量。这是无效的,因为函数参数默认为常量,用completionBlock声明。你所需要做的就是消除你所指的模糊性

class func animateWithDuration(... completionBlock: (() -> Void)? = nil, ... ) {
    ...
    self.completionBlock = completionBlock
    ...
}

10-08 07:29