0
0
iOS Swiftmobile~5 mins

List with ForEach in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of ForEach in SwiftUI?

ForEach is used to create multiple views by looping over a collection of data. It helps display lists or repeated UI elements dynamically.

Click to reveal answer
beginner
How do you identify each item uniquely in a ForEach loop?

You provide a unique identifier for each item, usually by conforming your data to Identifiable protocol or by specifying an id key path.

Click to reveal answer
intermediate
What is the difference between List and ForEach in SwiftUI?

List creates a scrollable list container, while ForEach generates views for each data item. ForEach can be used inside List to build the rows.

Click to reveal answer
intermediate
Why should data used in ForEach be uniquely identifiable?

SwiftUI uses the unique ID to track and update views efficiently when data changes, preventing UI glitches or wrong updates.

Click to reveal answer
beginner
Show a simple SwiftUI code snippet using List and ForEach to display a list of names.
struct ContentView: View {
  let names = ["Anna", "Brian", "Craig"]
  var body: some View {
    List {
      ForEach(names, id: \.self) { name in
        Text(name)
      }
    }
  }
}
Click to reveal answer
What does ForEach require to uniquely identify each element?
AA unique ID for each element
BA color for each element
CA font size for each element
DA background image for each element
Which SwiftUI view creates a scrollable list container?
AForEach
BText
CList
DButton
Can ForEach be used inside a List?
ANo, they are mutually exclusive
BYes, to generate rows dynamically
COnly outside of <code>List</code>
DOnly with static data
What happens if data in ForEach is not uniquely identifiable?
AUI updates may be incorrect or glitchy
BThe app runs faster
CSwiftUI crashes immediately
DNothing, it works fine
Which protocol helps data to be uniquely identified in SwiftUI?
ACodable
BObservableObject
CEquatable
DIdentifiable
Explain how ForEach works in SwiftUI and why unique identification of data is important.
Think about how SwiftUI updates the screen when data changes.
You got /4 concepts.
    Describe the relationship between List and ForEach in building a dynamic list UI.
    Imagine making a contact list app with many names.
    You got /4 concepts.