새 항목 추가하기
행을 추가하고 목록을 동기화된 상태로 유지합니다.
새 항목 추가하기은(는) CoddyKit의 무료 SwiftUI Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 SwiftUI Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. SwiftUI Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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. 🎉
자주 묻는 질문
“새 항목 추가하기” 강의는 무료인가요?
네 — “새 항목 추가하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 SwiftUI Academy 강의 전체를 잠금 해제할 수 있습니다. SwiftUI Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“새 항목 추가하기”에서 뭘 배우나요?
행을 추가하고 목록을 동기화된 상태로 유지합니다. 브라우저에서 직접 실행하는 실습 코드로 SwiftUI Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
SwiftUI Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 SwiftUI Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“새 항목 추가하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 SwiftUI Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 SwiftUI Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 밀어서 행 삭제하기
- onMove로 순서 변경하기
- 새 항목 추가하기
- 당겨서 새로 고침