问题
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!
回答1:
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.
回答2:
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.
回答3:
Here's a real-world example using a common setUpTable
function.
Swift 2: C-Style Loop
func setupTable() {
tableView.setNumberOfRows(nameArray.count, withRowType: "ContactListCell")
for i in 0 ..< nameArray.count += 1 {
let cell = tableView.rowController(at: Int(i)) as? ContactListCell
let nameText = nameArray[i]
cell!.nameLabel.setText(nameText)
}
}
Error: left side of mutating operator isn't mutable "..<" returns immutable value
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.
for i in 0 ..< nameArray.count {}
来源:https://stackoverflow.com/questions/40388873/mutating-operator-error-after-changing-to-swift-3-issue-researched-but-cant-s