https://github.com/fluidgroup/swift-indexed-collection
https://github.com/fluidgroup/swift-indexed-collection
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/fluidgroup/swift-indexed-collection
- Owner: FluidGroup
- Created: 2024-07-01T17:41:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-12T01:26:39.000Z (almost 2 years ago)
- Last Synced: 2025-06-29T23:34:40.209Z (about 1 year ago)
- Language: Swift
- Size: 17.6 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# IndexedCollection
A wrapper collection that provides items with its index using underlying collection without allocation.
## Motivation
In SwiftUI, we might use these following technique for using index in `ForEach`.
```swift
ForEach(Array(array.enumerated()), id: \.offset) { ... }
```
```swift
ForEach(zip(array.indices, array), id: \.0) { ... }
```
There is downside like followings:
- Creating new buffer by making new collection
- `enumerated` provides index from 0 so that makes wrong access on using slice.
- animations won't work well since just using index.
## Usage
```swift
#Preview {
VStack {
ForEach.init(IndexedCollection([1, 2, 3, 4, 5]), id: \.index, content: { e in
Text("\(e.index): \(e.value)")
})
}
}
```
```swift
struct IdentifiableItem: Identifiable {
let id: String
let value: UUID = .init()
}
#Preview {
VStack {
ForEach.init(IndexedCollection(["a", "b", "c", "d", "e"].map(IdentifiableItem.init(id:))), content: { e in
Text("\(e.index): \(e.value)")
})
}
}
```
## Showcases
Using Group new API in iOS18. reversing z-index in a collection of view.
```swift
public struct ReversedZIndex: View {
private let content: Content
public init(@ViewBuilder content: () -> Content) {
self.content = content()
}
public var body: some View {
Group(
subviewsOf: content,
transform: { collection in
let count = collection.count
ForEach(IndexedCollection(collection), id: \.id) { element in
element.value
.zIndex(Double(count - element.index))
}
}
)
}
}
```