Skip to content

Instantly share code, notes, and snippets.

@antonmartinsson
Last active August 9, 2022 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonmartinsson/2e40c245820bb903119a3ca2d9faa5c9 to your computer and use it in GitHub Desktop.
Save antonmartinsson/2e40c245820bb903119a3ca2d9faa5c9 to your computer and use it in GitHub Desktop.
EnumeratedForEach
import SwiftUI
/// An extension of ForEach that enables lookup of an array offset parameter in its ViewBuilder closure, in addition to the standard element returned.
extension ForEach where Data: RandomAccessCollection, Data.Element: Hashable, ID == Data.Element, Content: View {
init(_ values: Data?, @ViewBuilder content: @escaping (Data.Index, Data.Element) -> Content) {
self.init(values ?? ([Data.Element]() as! Data), id: \.self) { c in
content(values?.firstIndex(of:c)! ?? ([Data.Element]() as! Data).indices.first!, c)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment