Running Code on Tap
Print, count, and trigger logic from a tap.
Running Code on Tap 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.
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. ⚡
Frequently asked questions
Is the “Running Code on Tap” lesson free?
Yes — the full text of “Running Code on Tap” 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 “Running Code on Tap”?
Print, count, and trigger logic from a tap. 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 “Running Code on Tap” 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
- Creating a Tappable Button
- Styling Buttons
- Running Code on Tap
- Disabling & Enabling Buttons