Last active
August 9, 2022 08:34
-
-
Save antonmartinsson/2e40c245820bb903119a3ca2d9faa5c9 to your computer and use it in GitHub Desktop.
EnumeratedForEach
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 | |
/// 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