Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between List and ForEach in SwiftUI?

Tags:

swiftui

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?

like image 946
Chéyo Avatar asked Jun 11 '19 01:06

Chéyo


People also ask

What is ForEach in SwiftUI?

A ForEach instance iterates over the array, producing new Text instances that display examples of each SwiftUI Font style provided in the array.

What is difference between ForEach and for loop in Swift?

To sum up: Using a for loop gives us a much greater degree of control over an iteration, while using forEach enables us to take advantage of the power of closures and first class functions, even though we won't be able to stop an iteration once it was started (apart from throwing an error, that is).

How do I create a list in SwiftUI?

Probably the simplest way to build a list is to create a new SwiftUI view and wrap the Hello World text in a List: struct StaticListView: View { var body: some View { List { Text("Hello, world!") } } } To add more items to the list, we can just add another line: List { Text("Hello, world!") Text("Hello, SwiftUI!") }

Can you use for loops in SwiftUI?

It is not possible to use procedural looping mechanisms such as For and While loops to layout views in SwiftUI. The SwiftUI framework provides the ForEach struct to accomplish an alternative to repeatedly add views to a view.


1 Answers

  • 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)       }   } 
like image 101
Mischa Avatar answered Sep 23 '22 12:09

Mischa