Skip to content

Instantly share code, notes, and snippets.

@dimitris-c
Last active April 11, 2018 15:12
Show Gist options
  • Save dimitris-c/a82e583eb1609f97dac45c25f9122ce3 to your computer and use it in GitHub Desktop.
Save dimitris-c/a82e583eb1609f97dac45c25f9122ce3 to your computer and use it in GitHub Desktop.
Converting an indexPath to the correct index of a one dimension array
let originalArray = [1, 2, 3, 4, 5, 6]
let sections = [[1, 2, 3], [5, 6]]
let indexPath = IndexPath(section: 0, row: 1)
let sectionTotal = self.sections.prefix(upTo: indexPath.section).map { $0.count }.reduce(0, +)
let index = sectionTotal + indexPath.row
let element = originalArray[index]
// element is `2`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment