更改为Swift 3后突变操作员错误,已研究但无法解决 [英] Mutating Operator Error After Changing to Swift 3, Issue Researched, but can't solve

查看:88
本文介绍了更改为Swift 3后突变操作员错误,已研究但无法解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到变异运算符的左侧不可更改".<返回不可变值"错误

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.

代码(和注释):

 //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
}

修改后的代码:

 //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
}

谢谢!

我在这里也遇到同样的错误....

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)
}

修订:

 //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)
}

谢谢!

推荐答案

引发错误的行是:

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

+=运算符是 muting运算符,这意味着它告诉Swift取走左侧的所有内容并对其进行更改,在这种情况下,添加右边的任何内容.

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.

所以您在这里告诉Swift的是,取0 ..< 2并将其更改为(0 ..< 2) + 1.这会引发错误,因为..<运算符返回的范围是不可变-一旦创建,就无法更改.

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.

即使您可以突变范围,也可能不是您想要的.从上下文看来,您可能想在右侧添加1,在这种情况下,您将摆脱=:

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{

这只是将右侧变成3,所以循环转到[0,1,2].

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

或者也许您只是想让它每次都增加1,就像标准C风格的for循环中的i += 1一样.如果是这种情况,您在这里不需要它.默认情况下,索引为1是默认行为,因此您可以省略整个+= 1位:

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 {

或者如果您需要增加1以外的值,则可以使用可扩展协议,如下所示:

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) {

只需用适当的数字替换minmaxincrement.

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

这篇关于更改为Swift 3后突变操作员错误,已研究但无法解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆