0

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

1

1 Answer 1

3

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)
                }
            }
        }
Sign up to request clarification or add additional context in comments.

4 Comments

You should probably add tuple naming for best practice? tuple.0.1.1.2.3.1.2.1.1 gets really confusing really fast
@Vollan We shouldn't really be zipping at all; we should be looping through one array. The basic data model is faulty.
@matt Yes, i agree, but if the model can't be fixed, then zip and tuple is a good solution, but should be used in the best way possible, dont you agree?
@Vollan I'm not going to change what I wrote (I'm not even sure I've guess the OP's intentions correctly). If you want to submit your own answer, go ahead!

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.