Giter VIP home page Giter VIP logo

swift-indexed-collection's Introduction

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)")
    })
  }
}

Showcases

Using Group new API in iOS18. reversing z-index in a collection of view.

public struct ReversedZIndex<Content: View>: 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))
        }
      }
    )
  }
}

swift-indexed-collection's People

Contributors

muukii avatar johnestropia avatar

Stargazers

Keitaro Kawahara avatar Ryu avatar Bảo Hà. avatar 권동영 avatar Omar Ashraf avatar

Watchers

 avatar shima avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.