加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – 更改为Swift 3后的变异运算符错误,问题已研究,但无法

发布时间:2020-12-14 04:37:58 所属栏目:百科 来源:网络整理
导读:我得到一个“变异运算符的左侧不可变”..“返回不可变值”错误 我阅读了关于变异值的其他帖子,但我无法弄清楚这些解决方案是如何应用的. 代码(和评论): //populate array of 3 random numbers using correct answer and 2 incorrect choices func insertInt
我得到一个“变异运算符的左侧不可变”..<“返回不可变值”错误 我阅读了关于变异值的其他帖子,但我无法弄清楚这些解决方案是如何应用的. 代码(和评论):

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

谢谢!

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

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

=运算符是一个变异运算符,这意味着它告诉Swift采取它左侧的任何内容并进行更改,在这种情况下,通过添加右侧的内容.

所以你在这里告诉Swift的是,取0 ..< 2,并将其改为(0 ..< 2)1.这会引发错误,因为...< operator返回一个不可变的范围 - 一旦创建,就无法更改. 即使你可以改变一个范围,这可能不是你想要做的.从上下文看起来可能你想在右侧添加1,在这种情况下你只需要摆脱=:

for intJ in 0 ..< 2 + 1{

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

或者你可能只想告诉它每次增加1,就像标准C风格for循环中的i = 1一样.如果是这样的话,你不需要它.索引为1是默认行为,因此您可以省略整个= 1位:

for intJ in 0 ..< 2 {

或者如果你需要增加1以外的值,你可以使用Strideable protocol,它看起来像:

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

只需用适当的数字替换min,max和increment即可.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读