Daniel Mihaila
Daniel Mihaila

Reputation: 585

Left side of mutating operator isn't mutable '..<' returns immutable value

I developed an app in Swift 2.0 years ago, it ran smoothly. Today I updated the code to 3.0 (and later would to 4.0) but I am getting an issue saying "Left side of mutating operator isn't mutable '..<' returns immutable value" I can't seem to solve.

This answer of removing the '=' from '+=' did not work for me, hope someone can help me get this resolved.

for i: CGFloat in 0  ..< self.frame.size.width / (groundTexture.size().width) += 1 {

The full code looks like this:

func moveGroundFunc(_ duration: TimeInterval, piece: SKSpriteNode) {
    let groundMovingLeft = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: duration)

    let resetGround = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 0)

    let groundMovingLeftForever = SKAction.repeatForever(SKAction.sequence([groundMovingLeft, resetGround]))

    for i: CGFloat in 0  ..< self.frame.size.width / (groundTexture.size().width) += 1 {
        piece.size = CGSize(width: 1500, height: groundPiece.size.height)
        piece.position = CGPoint(x: i * piece.size.width, y: piece.size.height - 75)
    }

Upvotes: 3

Views: 13333

Answers (1)

Paulw11
Paulw11

Reputation: 114828

Your code is trying to mutate the result of an expression (using += 1); this isn't possible since anonymous results are constants and therefore immutable.

I am not sure why changing the += 1 to +1 didn't work, but you can simply split it into two lines. If you change the range operator from ..< to ... you can get rid of the +1 as well.

Your other problem is that you can't use a range with CGFloat - You need to use a type that implements SignedInteger. An Integer division may not give you the same result. If it doesn't then you could change the loop to use incrementing rather than a range.

let width = Int(self.frame.size.width / groundTexture.size().width)

for i in 0...width {
    piece.size = CGSize(width: 1500, height: groundPiece.size.height)
    piece.position = CGPoint(x: i * piece.size.width, y: piece.size.height - 75)
}

Upvotes: 3

Related Questions