Adding New Items
Append rows and keep the list in sync.
Adding New Items is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Growing the List
A useful list does not stay frozen. Letting users add items is the other half of editing, and it keeps your @State array in charge. ✨
Append to Your Array
Adding a row is just adding data. Call append on your @State array and SwiftUI inserts a fresh row automatically.
items.append(Item(name: "Bread"))A Toolbar Add Button
Most apps add items from a plus button. Put a Button in the toolbar that calls your add logic when tapped.
.toolbar {
Button("Add") { addItem() }
}Use a System Plus Icon
Swap the text for an SF Symbol so it reads as Add. The plus symbol is the universal cue users already understand.
Button {
addItem()
} label: {
Image(systemName: "plus")
}Writing addItem
Your addItem function creates a new model value and appends it. Keep it small and let the array drive the UI.
func addItem() {
items.append(Item(name: "New"))
}Add From a TextField
Often the new item comes from typing. Bind a TextField to a draft string, then append that text when the user confirms.
@State private var draft = ""
TextField("Item", text: $draft)Append Then Clear
After appending, reset the draft so the field is empty for the next entry. Clearing draft keeps the form feeling fresh.
items.append(Item(name: draft))
draft = ""Guard Against Blanks
Do not add empty rows. A quick guard on the trimmed draft stops accidental blank items from cluttering your list.
guard !draft.trimmingCharacters(
in: .whitespaces).isEmpty
else { return }New Rows Animate In
Because the array changed, SwiftUI slides the new row into place. Wrap the append in withAnimation for an even snappier feel.
withAnimation {
items.append(Item(name: draft))
}Insert at the Top
Want newest first? Use insert(at:) with index 0 instead of append, and the row appears at the top of the list.
items.insert(Item(name: draft),
at: 0)One Source of Truth
Add, delete, and move all mutate the same @State array. That single source of truth keeps your whole list consistent.
Quick Check
How do you add a new row to a SwiftUI list?
Recap: Adding Items
You can grow a list by appending to its @State array, often from a toolbar button or a TextField, with guards and animation. 🎉
Frequently asked questions
Is the “Adding New Items” lesson free?
Yes — the full text of “Adding New Items” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Adding New Items”?
Append rows and keep the list in sync. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Adding New Items” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Swipe-to-Delete Rows
- Reordering with onMove
- Adding New Items
- Pull-to-Refresh