Update Notes:
- In Xcode 26, you can interact with the button in the canvas to see the output in the console.
- To show the Debug Area, click View ▸ Debug Area ▸ Show Debug Area and then click Canvas at the bottom of the debug area.
It’s time to add some interactivity to the app! Let’s start by making the button simply print out a text message to the console when you tap it.
If you recall, when we added the button into bullseye we just put an open and closed curly brace here for the action which means when the button is tapped, run no code at all.
So we want to modify this to run a little bit of code.
We’ve got an open line, already, so right here type print all lower case and open and close parentheses. And then inside here we can put whatever we want to print out in quotes.
So I’m going to do open and close quote. And inside here, we’ll just write in “Hello, Swift UI”.
Button("Hit me") {
print("Hello, SwiftUI!")
}
We’re going to have to try this out in the simulator because currently there’s not an easy or reliable way to see the result of print statements from interacting with the canvas.
So, I’ll close up the canvas with the Adjust Editor Options button at the top right, and uncheck Canvas.
Now, we could click the play button in the upper right but instead, I’m going to hit command-R which is a shortcut to run the app in the simulator.
Once it’s running, I’m going to put it next to my full-screen Xcode. Just so we can look at both at the same time. You don’t have to do this if you don’t want to.
Your Mac might be set up differently than mine, but to do this I’m swiping up with four fingers on my trackpad, and then clicking and dragging the simulator over to the right of Xcode.
Now, go ahead and tap the “Hit me” button!
If you don’t see the console pop up automatically, you can click this button in the lower right here which expands it.
And you’ll notice now, here in Xcode we see “Hello, SwiftUI” printed out in this area at the bottom called the “console”.
I can tap hit me multiple times. And each time I click the button, I see this printed out.
To hide the debugger console again, you can use that button, or if you’re a keyboard shortcut person, this one is Shift-Command-Y.
print() is an example of a function. A function is like a method, except that it’s not attached to any instance. Not being attached to a class or struct means that you can use it without having to name an instance first.
In Swift, any time you see a name that starts with a lowercase letter that’s immediately followed by parentheses, such fontWeight() or print() — you’re probably looking at the name of a function or method. The difference is that methods are preceded by the instance that you’re calling the method on, while functions don’t belong to any instance and simply appear on their own.
The print() function takes some text and then prints it in Xcode’s Console, which is the pane in the lower right of Xcode that we saw earlier.
You can think of print() as a developer’s best friend. It’s very useful as a debugging tool, which means it’s purpose is to help you figure out what’s going on in your program, and to find the cause of any bugs you may have. You only see its results in Xcode, while you’re developing the app. If you take your app and ship it to the App Store, print() has no effect. It’s there only to help you out while you’re developing.
As you continue programming, you’ll find yourself using print() as an indicator to check that specific piece of code is being executed, or to check on some value that your program has stored. In the code you just wrote, you’re using print() to make sure that you run some code when you tap the button.
The fact pressing Hit me! in your app causes print() to print “Hello SwiftUI!” in the Console is a good sign. It means that you’ve proven you can make something happen when the button is pressed. Congratulations — you’ve just written some interactive code!
But you’re not done yet. Since print() is a debugging tool, users never sees their results. From their point of view, pressing Hit me! still does nothing. You still have to make the button provide a response that the user can see. And in order to do that, we need to go over the concept of SwiftUI state.