swift-indexed-collection

0.2.1

FluidGroup/swift-indexed-collection

What's New

0.2.1

2024-07-02T15:35:30Z

Full Changelog: 0.2.0...0.2.1

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.

ForEach(Array(array.enumerated()), id: \.offset) { ... }
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.

Usage

#Preview {
  VStack {
    ForEach.init(IndexedCollection([1, 2, 3, 4, 5]), id: \.index, content: { e in
      Text("\(e.index): \(e.value)")
    })
  }
}
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)")
    })
  }
}

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed May 14 2025 03:10:17 GMT-0900 (Hawaii-Aleutian Daylight Time)