0

This is the code:

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
}

This code works just fine, but I need to wrap the place where it inserts into the tempArray so that it doesn't add an empty string. Unfortunately, when I try to add an if statement, or place tempArray.insert(theHours, at: i) inside the already existing if statements, I get the error: "Swift/Array.swift:405: Fatal error: Array index is out of range"

I mean, I'm actually adding more items without the if statement! Can anyone tell me how to fix this?

4
  • So you are trying to insert at the index which actually does not exist? Commented Dec 21, 2021 at 12:38
  • Not entirely clear what you are asking... you say "This code works just fine..." but you're not showing us what code does NOT work fine. Commented Dec 21, 2021 at 12:43
  • Oh sorry, I posted bad code. Look, I do this a lot, even though I've been trying to get good at this for years -- again, I'm sorry for messing up the archive. To clarify, the line "tempArray.insert(theHours, at: i)" actually works on an array without that index when it wasn't wrapped inside the if statements. It actually added new indices when with that line under index = index + i. It was basically affirming my false belief that I was using the correct method to push new elements to the array, which was causing a lot of confusion. Commented Dec 21, 2021 at 12:55
  • tempArray.insert(theHours, at: i) not "self.timeArray.append(theHours)" Commented Dec 21, 2021 at 12:56

2 Answers 2

1

When you look at the documentation of the insert function, it says the following about the i parameter:

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.

You need to insert the element to an existing index or add it to the end of the array. It might help to add a print statement to print index, i and the array you are inserting it in to see what is exactly going on.

Sign up to request clarification or add additional context in comments.

3 Comments

So apparently I am using it wrong. But why does it work unless its wrapped inside an if statement? Weird.
It's hard for me to exactly understand your code without more context. Perhaps elaborate a little in the opening post, show the code that is not working properly. Give an example of what you are expecting to happen, etc.
Sorry for the confusion, and I really appreciate your patience and graciousness in trying to answer. I actually gave code that DID error out. The line "self.timeArray.append(theHours)" errors when its inside an if statement. When I place it outside of the if statements, right before the index = index + 1 line it actually works just fine. The fact that it was doing that basically affirmed my belief that I was using the correct method on the array and led to a lot of confusion. :)
0

Still a bit confusing, but I think I understand what you're going for...

Suppose count is 5 ...

If it is 10 o'clock, you want an array result of:

[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]

If it is 15 o'clock, you want an array result of:

[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]

If it is 22 o'clock, you want it to "wrap around" and get an array result of:

[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]

(your self.parse24(theString: String(index)) may be formatting it a little different).

If that's the case, take a look at this:

    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

Edit

It's probably important that you understand why you were getting the Array index is out of range error.

You still didn't post the code that was causing the error, but I'm guessing it is something like this:

    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
    }

So, if we start at 22 o'clock, and count equals 5, your code does this:

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 ***

You get the out of range error because you didn't insert the empty string at [2], but i keeps incrementing.

2 Comments

Done, my friend. What was confusing the hell into me was that I was getting away with using tempArray.insert unless I put it in that if statement under the hours assignment. I thought I was doing the right thing because it was working outside the if statement. Still blows my mind...
@Lester - see the Edit to my answer. I think it's important that you understand this, so it no longer "blows your mind".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.