Skip to content

Instantly share code, notes, and snippets.

@YusukeHosonuma
Created December 4, 2022 03:31
Show Gist options
  • Save YusukeHosonuma/7acca14c22b38f1ed5a139ed686fc6c4 to your computer and use it in GitHub Desktop.
Save YusukeHosonuma/7acca14c22b38f1ed5a139ed686fc6c4 to your computer and use it in GitHub Desktop.
ForEachIndex (with Algorithms)
import SwiftUI
import Algorithms
struct ForEachIndex<Data: RandomAccessCollection, ID: Hashable, Content: View>: View {
typealias Element = IndexedCollection<Data>.Element
private var data: Data
private var id: KeyPath<Element, ID>
@ViewBuilder private var content: (Element) -> Content
init(
_ data: Data,
id: KeyPath<Element, ID>,
@ViewBuilder content: @escaping (Element) -> Content
) {
self.data = data
self.id = id
self.content = content
}
var body: some View {
ForEach(data.indexed(), id: id, content: content)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment