3

How to use two variables in for loop?

for j,k in zip(range(x,0,-1),range(y,-1,-1) 

I want to implement this in Swift.

1
  • Put the j, k into parentheses (j, k) Commented Aug 18, 2015 at 22:48

2 Answers 2

15

If your range is a python function, then the Swift-y solution will be:

    let x = 100
    let y = 99

    let rx = reverse(0...x)
    let ry = reverse(-1...y)

    for (j,k) in zip(rx, ry) {
        println(j, k)
    }
Sign up to request clarification or add additional context in comments.

Comments

4

if you're looping over a dictionary you can loop like this

for (key,value) in dictionary {
}

if an array etc. you're going to have to use a c style for loop just sub in whatever start and end indices you need

for var j = 0 , k = 0; j < 10 && k < 10; j++ , k++ {
}

EDIT

missed the zip in there. You can loop like this

for (j,k) in zip(range1, range2) {
}

1 Comment

Didn't know about this syntax. But note: C-style for statement has been removed in Swift 3

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.