{"id":21376519,"url":"https://github.com/fluidgroup/swift-indexed-collection","last_synced_at":"2025-07-13T10:31:06.704Z","repository":{"id":246781358,"uuid":"822728411","full_name":"FluidGroup/swift-indexed-collection","owner":"FluidGroup","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-12T01:26:39.000Z","size":18,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-29T23:34:40.209Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FluidGroup.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-01T17:41:06.000Z","updated_at":"2024-09-12T01:26:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"1c3a9aa5-3389-4e8f-86cf-531afd4f1469","html_url":"https://github.com/FluidGroup/swift-indexed-collection","commit_stats":null,"previous_names":["fluidgroup/swift-indexed-collection"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/FluidGroup/swift-indexed-collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-indexed-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-indexed-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-indexed-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-indexed-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FluidGroup","download_url":"https://codeload.github.com/FluidGroup/swift-indexed-collection/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-indexed-collection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265128203,"owners_count":23715621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-22T09:16:02.554Z","updated_at":"2025-07-13T10:31:06.443Z","avatar_url":"https://github.com/FluidGroup.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IndexedCollection\n\nA wrapper collection that provides items with its index using underlying collection without allocation.\n\n## Motivation\n\nIn SwiftUI, we might use these following technique for using index in `ForEach`.\n\n```swift\nForEach(Array(array.enumerated()), id: \\.offset) { ... }\n```\n\n```swift\nForEach(zip(array.indices, array), id: \\.0) { ... } \n```\n\nThere is downside like followings:\n- Creating new buffer by making new collection\n- `enumerated` provides index from 0 so that makes wrong access on using slice.\n- animations won't work well since just using index.\n\n## Usage\n\n```swift\n#Preview {\n  VStack {\n    ForEach.init(IndexedCollection([1, 2, 3, 4, 5]), id: \\.index, content: { e in\n      Text(\"\\(e.index): \\(e.value)\")\n    })\n  }\n}\n```\n\n```swift\nstruct IdentifiableItem: Identifiable {\n  let id: String\n  let value: UUID = .init()\n}\n\n#Preview {\n  VStack {\n    ForEach.init(IndexedCollection([\"a\", \"b\", \"c\", \"d\", \"e\"].map(IdentifiableItem.init(id:))), content: { e in\n      Text(\"\\(e.index): \\(e.value)\")\n    })\n  }\n}\n```\n\n## Showcases\n\nUsing Group new API in iOS18. reversing z-index in a collection of view.\n\n```swift\npublic struct ReversedZIndex\u003cContent: View\u003e: View {\n  \n  private let content: Content\n  \n  public init(@ViewBuilder content: () -\u003e Content) {\n    self.content = content()\n  }\n  \n  public var body: some View {\n    Group(\n      subviewsOf: content,\n      transform: { collection in\n        let count = collection.count\n        ForEach(IndexedCollection(collection), id: \\.id) { element in\n          element.value\n            .zIndex(Double(count - element.index))\n        }\n      }\n    )\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fswift-indexed-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluidgroup%2Fswift-indexed-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fswift-indexed-collection/lists"}