Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift3 – 更改为Swift 3后的变异运算符错误,问题已研究,但无法解决大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到一个“变异运算符的左侧不可变”..<“返回不可变值”错误 我阅读了关于变异值的其他帖子,但我无法弄清楚这些解决方案是如何应用的. 代码(和评论):

//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即可.

大佬总结

以上是大佬教程为你收集整理的swift3 – 更改为Swift 3后的变异运算符错误,问题已研究,但无法解决全部内容,希望文章能够帮你解决swift3 – 更改为Swift 3后的变异运算符错误,问题已研究,但无法解决所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。