Thuta Learning
IntermediateMobile Developmentintermediate

Lists (List, ForEach)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Lists (List, ForEach), without any of the intimidation
  • Get hands-on running Xcode/SwiftUI code yourself
  • Apply this concept immediately in a real project

Let's think about this for a moment

Using `ForEach` inside a `VStack` renders all child views at once — with 1,000 items, this can hurt performance (and you'd need to manually wrap it in a `ScrollView` to scroll). `List`, on the other hand, is a built-in scrollable container — it efficiently renders only the visible items (lazy loading, similar to Android's `LazyColumn`/RecyclerView), and it comes with built-in native iOS behaviors like swipe-to-delete and pull-to-refresh.

Let's connect this to a real-world scenario

In a todo list app, writing `List(todos) { todo in TodoRow(todo: todo) }` — even with 100 todo items, only the ones visible on screen get rendered, and you automatically get native iOS scroll/swipe behavior (like swipe-to-delete) — each item in `todos` needs to conform to the `Identifiable` protocol (via a `TodoItem`'s `id` property).

Let's look at it together

swift
struct TodoListScreen: View {
    let todos: [TodoItem]

    var body: some View {
        List(todos) { todo in
            HStack {
                Image(systemName: todo.isDone ? "checkmark.circle.fill" : "circle")
                Text(todo.title)
            }
        }
    }
}
You should see
You should see a scrollable list of 5-10 todo items on the Simulator, in native iOS style (including swipe-to-delete).

Try it in 5 minutes

Create an array of `TodoItem` (the struct from the Basic chapter) with 10 sample items, and run `TodoListScreen` — scroll through it and confirm it feels smooth.

A quick word of caution

If you want to remove/customize `List`'s default styling (row separators, background), you'll need the `.listStyle()` modifier — the default appearance might not match your design mockup.

Easy traps

  • Putting a TodoItem into a `List` without conforming it to the `Identifiable` protocol — you'll get a compile error, or need to manually supply an `id:` parameter
  • Defaulting to `List` even for a short list with just 5-6 items — for a small number of items, a plain `VStack` is simpler and more fitting

Now try it yourself

Create an array of `TodoItem` (the struct from the Basic chapter) with 10 sample items, and run `TodoListScreen` — scroll through it and confirm it feels smooth.

You'll know it worked when: You should see a scrollable list of 5-10 todo items on the Simulator, in native iOS style (including swipe-to-delete).

Lists (List, ForEach) | Thuta Learning