gcpreston
gcpreston

Reputation: 135

Swift: Exit nested for loop whilst maintaining outer loop's for index

I'm fairly new to Swift and I'm sure this is a relatively straight forward question. I have a nested for loop and I would like to break out of the inner loops after appending the data to an array. Below is my code:

            for set in self.setArray{
                self.itemData = "Set "
                self.itemData += set
                self.itemData += "  "
                for reps in self.repsArray{
                    self.itemData += reps
                    self.itemData += " Reps  "
                    for weight in self.weightArray{
                        self.itemData += "Weight "
                        self.itemData += weight
                        self.itemData += "kg"
                        structSetArray.append(self.itemData)
                        self.itemData = ""
                        break
                    }
                    break
                }
            }

Currently the break statements allow me to return back to the set array and retrieve the next item, however, this causes the reps and weight arrays to start from the beginning again. How can I prevent this from happening so all the loops retrieve the second item?

Thanks in advance

Upvotes: 0

Views: 282

Answers (1)

matt
matt

Reputation: 535284

It's hard to imagine what your intention might be (and you have not explained it), so I'll give two hypotheses with two different answers. (Note that in doing so I'm going to eliminate your self.itemData and replace it with a local variable.


Perhaps you're trying to loop thru three arrays at the same time. To do that, use zip and just one for loop.

This would be easier if zip3 existed natively; you could write it, but there's really no need:

let zippedArray = zip(setArray, zip(repsArray, weightArray))
for tuple in zippedArray {
    let set = tuple.0
    let reps = tuple.1.0
    let weight = tuple.1.1
    var itemData = "Set "
    itemData += set
    itemData += "  "
    itemData += reps
    itemData += " Reps  "
    itemData += "Weight "
    itemData += weight
    itemData += "kg"
    structSetArray.append(itemData)
}

In real life, however, it would be better to have just one array whose elements are a struct with three properties (set, reps, and weight).


On the other hand, it might be that you're trying to loop through all possible combinations of your three arrays. In that case, move all the "printing" of values into the innermost loop, like this:

        for set in self.setArray{
            for reps in self.repsArray{
                for weight in self.weightArray{
                    var itemData = "Set "
                    itemData += set
                    itemData += "  "
                    itemData += reps
                    itemData += " Reps  "
                    itemData += "Weight "
                    itemData += weight
                    itemData += "kg"
                    structSetArray.append(itemData)
                }
            }
        }

Upvotes: 3

Related Questions