0Pricing
SwiftUI Academy · Aula

Adicionando novos itens

Adicione linhas e mantenha a lista sincronizada.

Adicionando novos itens é uma aula grátis de SwiftUI Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de SwiftUI Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de SwiftUI Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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. 🎉

Perguntas Frequentes

A aula “Adicionando novos itens” é grátis?

Sim — o texto completo de “Adicionando novos itens” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de SwiftUI Academy, atualize para CoddyKit PRO. O curso de SwiftUI Academy inclui 4 aulas no total.

O que vou aprender em “Adicionando novos itens”?

Adicione linhas e mantenha a lista sincronizada. Você pratica SwiftUI Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar SwiftUI Academy?

Nenhuma experiência prévia é necessária. SwiftUI Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.

Quanto tempo leva a aula “Adicionando novos itens”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de SwiftUI Academy?

Sim. Cada aula de SwiftUI Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Linhas com deslize para excluir
  2. Reordenando com onMove
  3. Adicionando novos itens
  4. Deslize para atualizar
← Voltar para SwiftUI Academy