Created
December 4, 2022 03:31
-
-
Save YusukeHosonuma/7acca14c22b38f1ed5a139ed686fc6c4 to your computer and use it in GitHub Desktop.
ForEachIndex (with Algorithms)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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