iOS Tutorial

SwiftUI ForEach, Lists and Forms

Learn how to generate dynamic SwiftUI views with ForEach, build Lists from collections and dictionaries, sort data on the fly, and collect user input with Forms.

SwiftUIForEachListForm

SwiftUI for production apps

SwiftUI is widely used for production iOS apps and provides a concise, declarative way to build interfaces. Its syntax and data-driven approach differ from traditional UIKit development, so there is a learning curve, but ForEach, List, and Form are some of the most useful building blocks to understand early.

Step 1

Use ForEach to generate repeated views

SwiftUI's ForEach lets you iterate over collections and create a corresponding view for each item. It is useful for rows, grids, boards, menus, and other dynamic interfaces without manually repeating view code.

struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            ForEach(0..<8, id: \.self) { row in
                HStack(spacing: 0) {
                    ForEach(0..<8, id: \.self) { column in
                        Rectangle()
                            .fill((row + column).isMultiple(of: 2) ? .white : .black)
                    }
                }
            }
        }
        .aspectRatio(1, contentMode: .fit)
    }
}
View original example on GitHub Gist →
Checkers board created with nested SwiftUI ForEach loops

Nested ForEach loops make a checkers-style board straightforward: one loop creates rows while the inner loop creates the cells in each row.

Step 2

Build a SwiftUI List from a dictionary

ForEach can also iterate over dictionary data. In this example, player names are used as keys and their ranking values are displayed in a SwiftUI List.

struct ContentView: View {
    let players: [String: Int] = [
        "John": 4,
        "Sarah": 1,
        "David": 3,
        "Emma": 2
    ]

    var body: some View {
        List {
            ForEach(Array(players.keys), id: \.self) { name in
                HStack {
                    Text(name)
                    Spacer()
                    Text("Rank: \(players[name] ?? 0)").bold()
                }
            }
        }
    }
}
View original List example on GitHub Gist →
SwiftUI list of checkers players and rankings
iOS App Templates

Explore complete iOS app projects

Now that you’ve seen how SwiftUI can generate dynamic interfaces from data, these complete iOS app templates are useful starting points for exploring production-ready app structure.

Step 3

Sort dictionary values directly inside ForEach

You do not need a separate pre-sorted array. The dictionary can be sorted by rank as it is passed into ForEach, while the dictionary key acts as the unique identifier.

List {
    ForEach(
        players.sorted(by: { $0.value < $1.value }),
        id: \.key
    ) { name, rank in
        HStack {
            Text(name)
            Spacer()
            Text("Rank: \(rank)").bold()
        }
    }
}
View original sorted dictionary example on GitHub Gist →
Key idea: players.sorted(by:) produces the order you want, while id: \.key gives ForEach a stable identifier when each player name is unique.
Step 4

Add new players with a SwiftUI Form

A SwiftUI Form is a convenient container for data-entry controls. Here it adds a text field below the sorted list, then inserts a new player with a random rank when the user submits the field.

struct ContentView: View {
    @State var players: [String: Int] = [String: Int]()
    @State var playerName: String = ""

    var body: some View {
        VStack(spacing: 0) {
            List {
                ForEach(players.sorted(by: { $0.value < $1.value }), id: \.key) { name, rank in
                    HStack {
                        Text(name)
                        Spacer()
                        Text("Rank: \(rank)").bold()
                    }
                }
            }

            Form {
                TextField("Player name...", text: $playerName)
            }
            .onSubmit {
                players[playerName] = Int.random(in: 1...20)
                playerName = ""
            }
            .background(Color.accentColor)
            .scrollContentBackground(.hidden)
            .frame(height: 100)
        }
    }
}
View original Form example on GitHub Gist →
SwiftUI Form used to add players to a dynamic list

Combining ForEach, List, and Form gives you a compact pattern for displaying changing data and letting users add new values without building a large amount of interface code.

Conclusion

SwiftUI's ForEach, List, and Form work especially well together for dynamic, data-driven interfaces. ForEach generates the repeated content, List provides the structured scrolling presentation, and Form gives users a familiar way to enter or edit data.

These examples cover the fundamentals, but the same patterns can scale into much richer app interfaces as your models and interactions become more complex.

Need help building your iOS app?

Apps4World provides custom native iOS development in Swift, SwiftUI, and UIKit — from the first screen through App Store delivery.

Discuss your project