Hunter Hunter - 25 days ago 7
iOS Question

Cannot assign to value: 'completionBlock is a 'let' constant

Hey ever since a have moved to Swift 3 I get this error that says my completionBlock in my

SCNTransaction
extension is a let constant and im not sure how to change it to a "var" any help?

Picture of Error here

Code:

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

Answer

I think this problem has to do with the fact that the instance variable completionBlock of the class SCNTransaction shares the same name as the parameter completionBlock of your function. As a result, the line of code completionBlock = completionBlock is being interpreted as an assignment of a value to the parameter completionBlock, not the instance variable of the same name. This is invalid because function parameters are constants by default, declared with let. All you need to do is remove the ambiguity of which completionBlock you are referring to:

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