为什么我的数组索引超出使用 if 语句的范围?

Why Is My Array Index Out Of Range Using An If Statement?

这是代码:

func setTimeArray() {
    let iStart = Int(Double(selectedStart)! * 0.01)
    var index = iStart
    var tempArray: Array<String> = []

    print("count is ", count)
    for i in 0..<self.count  {
        var theHours = ""
        if (index == 24) {
           index = 0
        }  else if (index == 23) {
            theHours = self.parse24(theString: String(index)) + " to " + self.parse24(theString: "0")
        } else {
            theHours = self.parse24(theString: String(index)) + " to " + self.parse24(theString: String(index + 1))
        }
        tempArray.insert(theHours, at: i)
        index = index + 1
    }
    self.timeArray = tempArray
}

这段代码工作得很好,但我需要将它插入到 tempArray 中的地方包装起来,这样它就不会添加空字符串。不幸的是,当我尝试添加一个 if 语句,或将 tempArray.insert(theHours, at: i) 放在已经存在的 if 语句中时,我收到错误消息:“Swift/Array.swift: 405: 致命错误:数组索引超出范围"

我的意思是,我实际上是在不使用 if 语句的情况下添加更多项目!谁能告诉我如何解决这个问题?

当您查看 documentation of the insert function 时,它对 i 参数的说明如下:

i

The position at which to insert the new element. index must be a valid index of the array or equal to its endIndex property.

您需要将元素插入到现有的 index 或将其添加到数组的末尾。添加打印语句以打印 indexi 和您要插入它的数组以查看到底发生了什么可能会有所帮助。

仍然有点困惑,但我认为我明白你的意思...

假设 count5 ...

如果是 10 点钟,你想要一个数组结果:

[10:00 to 11:00]
[11:00 to 12:00]
[12:00 to 13:00]
[13:00 to 14:00]
[14:00 to 15:00]

如果是 15 点,你想要一个数组结果:

[15:00 to 16:00]
[16:00 to 17:00]
[17:00 to 18:00]
[18:00 to 19:00]
[19:00 to 20:00]

如果是22点钟,你希望它“环绕”并得到一个数组结果:

[22:00 to 23:00]
[23:00 to 0:00]
[0:00 to 1:00]
[1:00 to 2:00]
[2:00 to 3:00]

(您的 self.parse24(theString: String(index)) 格式可能有点不同)。

如果是这样,请看这个:

    var tempArray: Array<String> = []

    // no need for an "i" counter variable
    for _ in 0..<self.count  {
        var theHours = ""
        
        // if we're at 24, set index to 0
        if (index == 24) {
            index = 0
        }
        
        if (index == 23) {
            // if we're at 23, the string will be "23:00 to 0:00"
            theHours = "23:00 to 0:00"
        } else {
            // the string will be "index to index+1"
            theHours = "\(index):00 to \(index+1):00"
        }
        // don't use insert, just append the new string
        //tempArray.insert(theHours, at: i)
        tempArray.append(theHours)
        
        index = index + 1
    }
    
    self.timeArray = tempArray

编辑

了解为什么您会收到 Array index is out of range 错误可能很重要。

您仍然没有 post 导致错误的代码,但我猜它是这样的:

    for i in 0..<self.count  {
        var theHours = ""
        if (index == 24) {
            index = 0
        }  else if (index == 23) {
            theHours = self.parse24(theString: String(index)) + " to " + self.parse24(theString: "0")
        } else {
            theHours = self.parse24(theString: String(index)) + " to " + self.parse24(theString: String(index + 1))
        }
        
        if theHours.isEmpty {
            // don't add the empty string to the array
        } else {
            // add it to the array
            tempArray.insert(theHours, at: i)
        }
        
        index = index + 1
    }

所以,如果我们从 22 点 开始,并且 count 等于 5,您的代码会这样做:

i equals 0
index equals 22
theHours = "22 to 23"
insert string at [i]  // i is 0
increment index
increment i

i now equals 1
index now equals 23
theHours = "23 to 0"
insert string at [i]  // i is 1
increment index
increment i

i now equals 2
index now equals 24
    set index to 0
theHours = ""
DON'T insert empty string
increment i

i now equals 3
index now equals 0
theHours = "0 to 1"
insert string at [i]  // i is 3

*** ERROR ***

你得到了超出范围的错误,因为你没有在[2]处插入空字符串,但是[=22] =] 不断递增。