What is the difference between List and ForEach in SwiftUI?

Swiftui

Swiftui Problem Overview


I know SwiftUI does not support currently regular for loops but instead provide something called ForEach but what is the difference between that and a List?

Swiftui Solutions


Solution 1 - Swiftui

  • ForEach is a view that lets you pass a collection of data to its initializer and then creates multiple "subviews" from the closure you provide. It doesn't have any semantics on how the views will be arranged.

    Example:
      ForEach(1..<5) { row in
          Text("Row \(row)")
      }
    

    will create the equivalent off

      Text("Row 1")
      Text("Row 2")
      Text("Row 3")
      Text("Row 4")
    

    wrapped in a single container view.

  • List is a view that can compose multiple views together, but not necessarily views of the same type. You can simply add multiple views without any loop.

    Example 1:
      List {
          Image("avatar")
          Text("Title")
          Button(action: {
              print("Button tapped!")
          }) {
              Text("Energize!")
          }
      }
    

    As a convenience, the List initializer allows you to use it just like the ForEach view in case you want to have a list consisting of a single cell type only.

    Example 2:
      List(1..<5) { row in
          Text("Row \(row)")
      }
    

    A list has a special appearance, depending on the platform. For example, on iOS a list will appear as a table view and insert separator lines between its vertically stacked views.

    You can use ForEach views inside List views to have both dynamic and static content – a very powerful feature of SwiftUI.

    Example 3:
      List {
          Text("Food")
          ForEach(meals) { meal in
              Text(meal.name)
          }
          Text("Drinks")
          ForEach(drinks) { drink in
              Text(drink.name)
          }
      }
    

Solution 2 - Swiftui

List:

  • mixed content(also allows to work with collection)
  • scrollable
  • uses reusing cell pattern (efficient)

ForEach:

  • works only with a collection

List + ForEach = super feature. In this case, for example, List uses reusing cell pattern for every view from ForEach. Also you are able to use onMove, onDelete...

Solution 3 - Swiftui

Simple explanation:

  • List behaves like a UIScrollView (an arbitrarily long scrollable view of views)
  • ForEach behaves kind of like a UITableView (where each of the elements gets a cell view, and it's also scrollable)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCh&#233;yoView Question on Stackoverflow
Solution 1 - SwiftuiMischaView Answer on Stackoverflow
Solution 2 - SwiftuiyoAlex5View Answer on Stackoverflow
Solution 3 - Swiftuim0blView Answer on Stackoverflow