1

I was able to implement a for ... in loop in Swift for two variables using the zip() function:

for (i, j) in zip(1...number, 2...number + 1)

Now, I am trying to implement a for ... in loop in Swift for three variables using zip(), but it is giving errors:

for (i, j, k) in zip(1...number, 2...number + 1, 3...number + 2)

The error I am getting when using zip:

main.swift:8:51: error: extra argument in call for (i, j, k) in zip(1...number, 2...number + 1, 3...number + 2)

I also tried omitting the zip() function, but I am still getting errors:

for (i, j, k) in (1...number, 2...number + 1, 3...number + 2)

The error I am getting without using zip():

main.swift:8:18: error: type '(ClosedRange, ClosedRange, ClosedRange)' does not conform to protocol 'Sequence' for (i, j, k) in (1...number, 2...number + 1, 3...number + 2)

How can I fix this?

2
  • 1
    What's the error? Commented Nov 13, 2019 at 5:46
  • 1
    Please include the error(s) in your question, thanks! Commented Nov 13, 2019 at 5:46

1 Answer 1

2

zip(_:_:) doesn't work with more than 2 arguments.

func zip(_ sequence1: Sequence1, _ sequence2: Sequence2) -> Zip2Sequence where Sequence1 : Sequence, Sequence2 : Sequence

You need to combine multiple zip(_:_:) to get that working.

Example:

for ((i, j), k) in zip(zip(1...number, 2...number + 1), 3...number + 2) {
    print(i, j, k)
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.