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!
The line that's throwing the error is:
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
=
: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:Or if you need to increment by something other than 1, you can use the Strideable protocol, which looks like:
Just replace
min
,max
andincrement
with the appropriate numbers.Here's a real-world example using a common
setUpTable
function.Swift 2: C-Style Loop
Swift 3 Fix: When we setup tables we are iterating through cell rows i.e. ContactListCell so that we can display them in the table.
for i in 0 ..< nameArray.count
already indexes by 1 therefore we don't need the C-style+= 1
syntax.add
var
in for loop, will make left variable mutableAnd in your snippet,
for intIA in 1 ..< 11 += 1
here+=1
is incorrect.