← All daysDay 16 of 49 · Part 5: Lists and navigation
Day 16

List and ForEach

These are the lesson notes for this day. Its build-along tutorial and flash cards have not been written yet.

Lesson 18

List and ForEach

List shows scrolling rows with the system's styling, swipe actions, selection, and sections. It reuses rows for performance, as UITableView does. ForEach produces views from a collection, and can be used inside a List or inside any other container. Both need a stable, unique identity for every element: either make your type conform to Identifiable, or pass an id: key path. Using array indices, or \.self on values that are not unique, leads to wrong animations, lost state, and crashes when a row is deleted.

The everyday tools: .swipeActions for row actions, .onDelete and .onMove for editing, Section for grouping, and .listStyle for appearance. iOS 27 lets you reorder items in any container with .reorderable() and .reorderContainer(for:), and lets you use .swipeActions outside a List.

Exercise

Build a list of trips grouped into sections by year, with swipe-to-delete and a second swipe action that marks a trip as a favorite.

After this lesson you should be able to answer
  1. Why does ForEach need each element to have a stable identity?
  2. What goes wrong if you use array indices as ids and then delete a row?
  3. How do you add a custom swipe action to a row?
Resources