Staley Staley - 4 months ago 8
iOS Question

How to set a variable to start at 0 and gain a level through each loop in a nested for loop Swift 2

So this is an example of a nested for loop in swift 2. There is a for loop just above and just below this all working one right after the other. I am wanting to climb a level each and every time the for loop is called. Normally if this was a for loop on its on this would occur automatically but since it is a nested for loop I need it to run once and then jump to the next for loop.

The problem is with the var u being at 0 it is always set at 0 and will always start there. Is there a way to make it take on the form post place[u] = place[++]?

placeLoop: for aPlace in place {
print("\(aPlace)")
print(" ")
var u : Int = 0
if aPlace == place[u] {
place[u] = place[++u]
//This is the manual way to achieve what I want but I have 105 records I want to iterate through, there has to be a better way to do this.
//place[u] = var place[u]
//place[1] = place[2]
//place[2] = place[3]
//if aPlace is equal to place0 then 0 = 1, next loop 1 = 2, next loop 2 =3
//you can't ++ a "String which place[with an index] is.


I have been reading through this documentation and looked at many different stackoverflow questions but nothing has helped thus far...I was considering a switch statement but not sure if that will work any differently.

EDIT:

for a in coor {
for aPlace in place {
for aPass in pass {


}}}


data is in geojson format:
coor: double(2343.90)
place: String (" asdfsa ")
pass: String (" asdfkkrr ")

plus 3 more records

I need it to return for input further in my code in this order:
coor1, place1, and pass1 then coor2, place2 and pass2, etc through the data.

Answer

Define your u variable outside of your first for loop and u += 1 on the end of your outside loop

Check this

var u : Int = 0
placeLoop: for aPlace in place {
             print("\(aPlace)")
             print(" ")

             if aPlace == place[u] {
             place[u] = place[++u]
             //This is the manual way to achieve what I want but I have 105 records I want to iterate through, there has to be a better way to do this.
             //place[u] = var place[u]
             //place[1] = place[2]
             //place[2] = place[3]
             //if aPlace is equal to place0 then 0 = 1, next loop 1 = 2, next loop 2 =3
             //you can't ++ a "String which place[with an index] is.
             u += 1

I hope this helps you, Regards

Comments