Hunter Hunter - 3 months ago 26
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

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

Picture of Error here


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


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