将代码更新为最新的Swift 4语法会导致“线程1:致命错误:索引超出范围”错误

时间:2021-06-10 22:42:36

I'm currently in the process of updating some apps to the latest Swift 4 syntax and have run into a problem, and I'm sure I'm just missing something really obvious.

我目前正在使用最新的Swift 4语法更新一些应用程序并遇到问题,我确信我只是遗漏了一些非常明显的东西。

Initially my code was as follows:

最初我的代码如下:

    let indices : [Int] = [0,1,2,3]
    //let newSequence = shuffle(indices)
    let newSequence = indices.shuffle()
    var i : Int = 0
    for(i = 0; i < newSequence.count; i++)
    {
        let index = newSequence[i]
        if(index == 0)
        {
            // we need to store the correct answer index
            currentCorrectAnswerIndex =  i

        }

Initially I had three error with the above code, namely:

最初我在上面的代码中有三个错误,即:

  1. Value of type '[Int]' has no member 'shuffle'
  2. 类型'[Int]'的值没有成员'shuffle'

  3. C-style for statement has been removed in Swift 3
  4. 在Swift 3中删除了C-style for语句

  5. Unary operator '++' cannot be applied to an operand of type '@lvalue Int'
  6. 一元运算符'++'不能应用于'@lvalue Int'类型的操作数

To address these I changed the code so that it's now as follows:

为了解决这些问题,我更改了代码,现在它如下:

    let indices : [Int] = [0,1,2,3]
    //let newSequence = shuffle(indices)
    let newSequence = indices.shuffle()
    var i : Int = 0
    while i < newSequence.count {i += 1

        let index = newSequence[i]
        if(index == 0)
        {
            // we need to store the correct answer index
            currentCorrectAnswerIndex =  i

        }

After changing the code and running a Product > Clean within Xcode, I no longer have any errors. However, when I use the app in the iOS Simulator it hangs at a certain point and checking within Xcode gives me the Thread 1: Fatal error: Index out of range error on the following line of code:

更改代码并在Xcode中运行Product> Clean后,我不再有任何错误。但是,当我在iOS模拟器中使用该应用程序时,它会在某个点挂起并在Xcode中检查给出了以下代码行中的线程1:致命错误:索引超出范围错误:

let index = newSequence[I]

I've read through other questions about the same error in Swift (see: 1, 2, 3 and 4) but these don't seem to apply in my scenario.

我已经阅读了有关Swift中相同错误的其他问题(参见:1,2,3和4),但这些似乎并不适用于我的场景。

I know I'm missing something really obvious here, but at present it's just escaping me.

我知道我在这里遗漏了一些非常明显的东西,但目前它只是逃避了我。

Any thoughts?

4 个解决方案

#1


2  

The Swift 3+ equivalent of the code is

Swift 3+代码相当于

let indices : [Int] = [0,1,2,3]
let newSequence = indices.shuffle() // this seems to be a custom function

for i in 0..<newSequence.count
{
    let index = newSequence[i]
    if index == 0 { // no parentheses in Swift
        // we need to store the correct answer index
        currentCorrectAnswerIndex = i
    }

}

#2


2  

The reason for crash is that you increment i, perform some operations with it, and only then check if it is not out of range. To get rid of this error just move i += 1 to the end of your closure.

崩溃的原因是你增加i,用它执行一些操作,然后检查它是否不在范围之外。要摆脱这个错误,只需将i + = 1移动到闭包的末尾即可。

But why not use fast enumeration? You're still trying to do a loop the old way, but with new syntax. Instead of doing it C way

但为什么不使用快速枚举?你仍然试图以旧的方式循环,但使用新的语法。而不是做C方式

var i : Int = 0
while i < newSequence.count {
    let index = newSequence[i]
    if(index == 0)
    {
        // we need to store the correct answer index
        currentCorrectAnswerIndex =  i

    }

    i += 1
}

do it the correct Swift way

做正确的Swift方式

for (index, value) in newSequence.enumerated() {
    if value == 0 {
        currentCorrectAnswerIndex =  index
    }
}

#3


2  

Change position of (incrementing value) i as shown in below code. (Value of i is increment before it is used in loop operations, hence during final/last iteration of loop, value of i becomes larger than array indices)

改变(递增值)i的位置,如下面的代码所示。 (i的值在循环操作中使用之前是递增的,因此在循环的最后/最后一次迭代期间,i的值变得大于数组索引)

let indices : [Int] = [0,1,2,3]
//let newSequence = shuffle(indices)
let newSequence = indices.shuffle()
var i : Int = 0
while i < newSequence.count {

    let index = newSequence[i]
    if(index == 0)
    {
        // we need to store the correct answer index
        currentCorrectAnswerIndex =  i

    }
    i += 1 // Update (increment) value for i at last
}

Update

(As suggested by MartinR) This would be better than you did:

(正如MartinR所建议的那样)这比你做得更好:

let indices : [Int] = [0,1,2,3]
let newSequence = indices.shuffle()

for (i, newSeqIndex) in newSequence.enumerated() {
    if (newSeqIndex == 0) {
        currentCorrectAnswerIndex =  i
    }

}

#4


0  

You put a i += 1 in there that isn't in the original code.

你在那里放了一个不在原始代码中的i + = 1。

#1


2  

The Swift 3+ equivalent of the code is

Swift 3+代码相当于

let indices : [Int] = [0,1,2,3]
let newSequence = indices.shuffle() // this seems to be a custom function

for i in 0..<newSequence.count
{
    let index = newSequence[i]
    if index == 0 { // no parentheses in Swift
        // we need to store the correct answer index
        currentCorrectAnswerIndex = i
    }

}

#2


2  

The reason for crash is that you increment i, perform some operations with it, and only then check if it is not out of range. To get rid of this error just move i += 1 to the end of your closure.

崩溃的原因是你增加i,用它执行一些操作,然后检查它是否不在范围之外。要摆脱这个错误,只需将i + = 1移动到闭包的末尾即可。

But why not use fast enumeration? You're still trying to do a loop the old way, but with new syntax. Instead of doing it C way

但为什么不使用快速枚举?你仍然试图以旧的方式循环,但使用新的语法。而不是做C方式

var i : Int = 0
while i < newSequence.count {
    let index = newSequence[i]
    if(index == 0)
    {
        // we need to store the correct answer index
        currentCorrectAnswerIndex =  i

    }

    i += 1
}

do it the correct Swift way

做正确的Swift方式

for (index, value) in newSequence.enumerated() {
    if value == 0 {
        currentCorrectAnswerIndex =  index
    }
}

#3


2  

Change position of (incrementing value) i as shown in below code. (Value of i is increment before it is used in loop operations, hence during final/last iteration of loop, value of i becomes larger than array indices)

改变(递增值)i的位置,如下面的代码所示。 (i的值在循环操作中使用之前是递增的,因此在循环的最后/最后一次迭代期间,i的值变得大于数组索引)

let indices : [Int] = [0,1,2,3]
//let newSequence = shuffle(indices)
let newSequence = indices.shuffle()
var i : Int = 0
while i < newSequence.count {

    let index = newSequence[i]
    if(index == 0)
    {
        // we need to store the correct answer index
        currentCorrectAnswerIndex =  i

    }
    i += 1 // Update (increment) value for i at last
}

Update

(As suggested by MartinR) This would be better than you did:

(正如MartinR所建议的那样)这比你做得更好:

let indices : [Int] = [0,1,2,3]
let newSequence = indices.shuffle()

for (i, newSeqIndex) in newSequence.enumerated() {
    if (newSeqIndex == 0) {
        currentCorrectAnswerIndex =  i
    }

}

#4


0  

You put a i += 1 in there that isn't in the original code.

你在那里放了一个不在原始代码中的i + = 1。