1

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.

Staley
  • 55
  • 2
  • 11
  • Define your u variable outside of your first for loop and u += 1 on the end of your outside loop – Reinier Melian Jul 12 '16 at 18:14
  • If you want finer control over the index, try this: http://stackoverflow.com/a/36166965/3141234 – Alexander Jul 12 '16 at 18:23
  • Can you explain more the algorithm that you want to achieve ? – Alaeddine Jul 12 '16 at 18:56
  • @Reinier what you offered works for the place situation but now pass is only updating with pass1 for each instance...so you answered my question but now I have another one. If you want to post an answer I'll accept it. – Staley Jul 12 '16 at 19:04
  • @Aladin I updated my question in hopes that additional info helps you understand a bit more clearly. – Staley Jul 12 '16 at 19:05
  • @AMomchilov the stride method seems interesting, I'll have to do some research on that. I have seen it before in my searches for help just wasn't sure it would apply here. – Staley Jul 12 '16 at 19:07
  • Every coord has only one place and each place has only one pass? – Reinier Melian Jul 12 '16 at 19:51
  • Yes exactly. I figured it out. Thanks so much for your help. – Staley Jul 12 '16 at 21:01

1 Answers1

1

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

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55