Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutating Operator Error After Changing to Swift 3, Issue Researched, but can't solve

I am getting a "left side of mutating operator isn't mutable "..<" returns immutable value" error

I read the other post regarding mutating values but I can't figure out how those solutions apply.

Code (and comments):

 //populate array of 3 random numbers using correct answer and 2 incorrect choices

 func insertIntoArray3(_ randomNumber: Int) -> Int {
for intJ in 0 ..< 2 += 1{

    if arrayIndex != 3 {
        checkIfExists(randomNumber)
        if ifExists {
            let randomNumber = 1 + random() % 10
            insertIntoArray3(randomNumber)
        } else {
            array3[arrayIndex] = (randomNumber)
            arrayIndex = arrayIndex + 1
        }
    }

}
return randomNumber
}

Revised Code:

 //populate array of 3 random numbers using correct answer and 2 incorrect choices
func insertIntoArray3(_ randomNumber: Int) -> Int {

    for _ in 0 ..< 2 + 1{

        if arrayIndex != 3 {
            checkIfExists(randomNumber)
            if ifExists {
                let randomNumber = 1 + arc4random() % 10
                insertIntoArray3(Int(randomNumber))
            } else {
                array3[arrayIndex] = (randomNumber)
                arrayIndex = arrayIndex + 1
            }
        }

    }
    return randomNumber
}

Thank you!

I'm getting the same error here too....

//this function populates an array of the 40 image names

func populateAllImagesArray() {
for  intIA in 1 ..< 11 += 1 {

    tempImageName = ("\(intIA)")
    imageArray.append(tempImageName)

    tempImageName = ("\(intIA)a")
    imageArray.append(tempImageName)

    tempImageName = ("\(intIA)b")
    imageArray.append(tempImageName)

    tempImageName = ("\(intIA)c")
    imageArray.append(tempImageName)
}

//println("imageArray: \(imageArray) ")
//println(imageArray.count)
}

Revised:

 //this function populates an array of the 40 image names
func populateAllImagesArray() {

    for  intIA in 1 ..< 11 + 1 {

        tempImageName = ("\(intIA)")
        imageArray.append(tempImageName)

        tempImageName = ("\(intIA)a")
        imageArray.append(tempImageName)

        tempImageName = ("\(intIA)b")
        imageArray.append(tempImageName)

        tempImageName = ("\(intIA)c")
        imageArray.append(tempImageName)
    }

    //println("imageArray: \(imageArray) ")
    //println(imageArray.count)
}

Thank you!

like image 982
Jimmy Maguire Avatar asked Nov 02 '16 20:11

Jimmy Maguire


2 Answers

The line that's throwing the error is:

for intJ in 0 ..< 2 += 1{

The += operator is a mutating operator, meaning that it tells Swift to take whatever is on the left side of it and change it, in this case by adding whatever is on the right.

So what you're telling Swift here is, take 0 ..< 2 and change it into (0 ..< 2) + 1. This throws an error because the ..< operator returns a range which is immutable - once created, it can't be changed.

Even if you could mutate a range, that's probably not what you want to do. From the context it looks like maybe you want to add 1 to the right side, in which case you'd just get rid of the =:

for intJ in 0 ..< 2 + 1{

This just turns the right side into 3, so the loop goes [0, 1, 2].

Or maybe you're just trying to tell it to increment by 1 every time, like the i += 1 in a standard C-style for loop. If that's the case you don't need it here. Indexing by 1 is the default behavior, so you can leave out the whole += 1 bit:

for intJ in 0 ..< 2 {

Or if you need to increment by something other than 1, you can use the Strideable protocol, which looks like:

for intJ in stride(from: min, to: max, by: increment) {

Just replace min, max and increment with the appropriate numbers.

like image 80
Robert Avatar answered Dec 10 '22 17:12

Robert


add var in for loop, will make left variable mutable

for var intIA in 1 ..< 11 {

    intIA = intIA+3
    print("\(intIA)")
}

And in your snippet, for intIA in 1 ..< 11 += 1 here +=1 is incorrect.

like image 44
Ankit Thakur Avatar answered Dec 10 '22 16:12

Ankit Thakur