无法赋值:'completionBlock是'let'常量

时间:2016-11-13 19:02:47

标签: swift scenekit

嘿,自从我搬到Swift 3后,我得到了这个错误,说我的SCNTransaction扩展名中的completionBlock是一个常量,我不确定如何将它更改为“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()
   }
}

1 个答案:

答案 0 :(得分:3)

我认为这个问题与类completionBlock的实例变量SCNTransaction与函数的参数completionBlock共享的事实有关。因此,代码行completionBlock = completionBlock被解释为对参数completionBlock的值的赋值,而不是同名的实例变量。这是无效的,因为默认情况下函数参数是常量,用let声明。您需要做的就是消除您所指的completionBlock的歧义:

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