Eseguire codice al tocco
Stampare, contare e attivare la logica in risposta a un tocco.
Eseguire codice al tocco è una lezione SwiftUI Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento SwiftUI Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso SwiftUI Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Action Is Yours
The closure inside a button is where your app does real work. This is where you run logic, update data, or kick off a task on tap. ⚡
Printing for a Quick Test
The simplest action is a print. It writes to Xcode's console so you can confirm a tap was registered while you build.
Button("Test") {
print("Button was tapped")
}Changing a Value
Most actions change some data. Here we bump a count each tap. Soon you will store that value in state so the UI can react.
Button("Add one") {
count += 1
}Calling Your Own Function
Keep buttons readable by moving logic into a function. The action becomes a single clear line that names what happens.
Button("Submit") {
submitForm()
}Multiple Steps in One Tap
An action can run several lines in order. SwiftUI runs them top to bottom, so a single tap can do a small sequence of work.
Button("Reset") {
score = 0
message = "Cleared"
}Conditional Logic on Tap
You can branch inside the action with an if. The tap decides what to do based on the current values of your data.
Button("Toggle") {
if isOn { turnOff() } else { turnOn() }
}Reading and Updating Together
A common pattern reads a value, computes something, then stores it back. The tap captures the user's intent in one place.
Button("Double it") {
total = total * 2
}Starting Async Work
For slow work like a network call, wrap it in a Task so the tap stays responsive while the work runs in the background.
Button("Load") {
Task { await fetchData() }
}Keep the Action Light
Buttons feel snappy when actions stay short. Hand heavy work to a function or Task instead of writing long logic inline.
One Tap, One Run
The action runs once per tap. If you need repeats, you control that in your logic. SwiftUI never fires the action twice for one tap.
Debugging a Silent Button
If a tap seems to do nothing, drop a print at the top of the action. Seeing it in the console confirms the button is wired correctly.
Button("Why?") {
print("I ran")
doWork()
}Quick Check
Let's check how tap actions behave.
Recap
Well done! A tap can print, change values, branch with if, or start a Task. Keep actions light and call functions for bigger jobs. ⚡
Domande Frequenti
La lezione «Eseguire codice al tocco» è gratuita?
Sì — il testo completo di «Eseguire codice al tocco» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso SwiftUI Academy, passa a CoddyKit PRO. Il corso SwiftUI Academy include 4 lezioni in totale.
Cosa imparerò in «Eseguire codice al tocco»?
Stampare, contare e attivare la logica in risposta a un tocco. Eserciti SwiftUI Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare SwiftUI Academy?
Non è richiesta alcuna esperienza precedente. SwiftUI Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Eseguire codice al tocco»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione SwiftUI Academy?
Sì. Ogni lezione SwiftUI Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Creare un pulsante selezionabile
- Applicare stili ai pulsanti
- Eseguire codice al tocco
- Disabilitare e abilitare i pulsanti