menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

Using enum...
source image

Lostmoa

6d

read

232

img
dot

Image Credit: Lostmoa

Using enumerated() with SwiftUI List and ForEach to show item numbers

  • Enumerated() in SwiftUI can be used with List and ForEach to show item numbers.
  • It returns a sequence of (offset, element) pairs, where offset is a counter from zero.
  • In Swift 6.2 and iOS 26, EnumeratedSequence now conforms to RandomAccessCollection.
  • Previously, an additional step was required to wrap the sequence in an array.
  • The offset in EnumeratedSequence should not be used as an index to access elements from the original collection.
  • Using offset or index as the id in ForEach is not recommended in dynamic collections.
  • For dynamic collections, a stable and unique identifier like Identifiable should be used.
  • A struct can be used with Identifiable to maintain stable identity in dynamic collections.
  • By decoupling numbering from identity, correct behavior is ensured as the data changes.
  • SwiftUI Fundamentals book is recommended for a deep dive into SwiftUI core principles and APIs.
  • For more resources on Swift and SwiftUI, other books and book bundles are available.

Read Full Article

like

13 Likes

For uninterrupted reading, download the app