Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created December 31, 2021 11:50
Show Gist options
  • Save cemolcay/61c2c422cf64c74c6507c1317de396d1 to your computer and use it in GitHub Desktop.
Save cemolcay/61c2c422cf64c74c6507c1317de396d1 to your computer and use it in GitHub Desktop.
Get item with row and col from 1d array
extension Collection where Self.Index == Int {
subscript(row: Int, col: Int, colCount: Int) -> Element? {
let index = row * colCount + col
guard index >= 0, index < count else { return nil }
return self[index]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment