this means war this means war - 1 month ago 7
iOS Question

in a forEach closure loop with two iteration variables, what does each variable represent?

I'm learning Swift and trying to make sense of this forEach loop. I fully understand the single iteration loop:

stackVC.forEach { i in
// do something

whereby for each iteration (i) in the loop, do something is executed. But in the case below, what is "i" and what is "j"? For context, stackVC is an array of UIViewController's that are being put together to go into a container view controller.

stackVC.enumerated().forEach { i, j in
let pageViewController = UIViewController()
j.view.frame = pageViewController.view.bounds
j.view.autoresizingMask = [
j.didMove(toParentViewController: pageViewController)

Answer Source

When using enumerated.forEach you get the offset and the element.

Note, as per Martin’s comment. The offset is different from the index and so may not necessarily be the actual index.