ForEach によるループ
データの配列から行を表示します。
「ForEach によるループ」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why ForEach
Hardcoding rows works for two items, but real data lives in arrays. ForEach builds one row per element automatically. 🔁
let fruits = ["Apple", "Pear", "Kiwi"]ForEach Inside List
Drop a ForEach inside a List and it generates one row for every item in your collection, no matter how many there are.
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}The Closure Gives You Each Item
The closure runs once per element, handing you that item so you can build its row from real data.
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}Identity Matters
SwiftUI needs to tell rows apart, so ForEach always wants a way to identify each element uniquely.
The id Parameter
For simple value arrays, pass id set to a key path like .self so each value can identify itself uniquely to SwiftUI.
ForEach(scores, id: \.self) { score in
Text("\(score)")
}Looping a Range
Need a fixed count instead of an array? Pass a range and ForEach hands you each index in turn, perfect for placeholder rows.
ForEach(0..<5) { index in
Text("Row \(index)")
}Building Rich Rows
Inside the closure you can return any view, so each ForEach row can be a full HStack with icons and labels.
ForEach(fruits, id: \.self) { fruit in
HStack {
Image(systemName: "leaf")
Text(fruit)
}
}Mixing Static and Dynamic
A List can hold a static header Text and a ForEach loop together, blending fixed and data-driven rows.
List {
Text("Fruits")
ForEach(fruits, id: \.self) { Text($0) }
}Looping Structs
When you loop an array of structs, each item carries multiple fields you can show across the row.
ForEach(people, id: \.name) { person in
Text(person.name)
}Data Drives the UI
Change the array and the rows follow right along. With ForEach, your list is simply a live mirror of whatever data you give it.
Beyond Lists
ForEach is not just for lists. It also fills VStack, HStack, and grids with repeated views from any collection.
VStack {
ForEach(fruits, id: \.self) { Text($0) }
}Quick Check
Recall what the ForEach closure receives.
Recap
You learned ForEach: it turns any collection into rows, handing you each item so your UI tracks your data automatically. 🎉
よくある質問
「ForEach によるループ」レッスンは無料ですか?
はい。「ForEach によるループ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「ForEach によるループ」で何を学びますか?
データの配列から行を表示します。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「ForEach によるループ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 静的な List の作成
- ForEach によるループ
- Identifiable と安定した ID
- セクションと List のスタイル