App 1 — 24 Apps You Can Make Today
Hello World
The very first app — a Text view, a Button, and your first taste of @State.
- ios
Every programmer's first program says hello. Yours will too — but it'll also say whatever you want, because you're going to make the button change it.
What you'll build
An app with a message on screen and a button below it. Tap the button and the message changes. Simple, readable, and already more interesting than printing to a terminal.
Key concepts
Text — The most basic SwiftUI view. Give it a String, it shows up on screen.
Text("Hello, world!")
.font(.largeTitle)
@State — When you want a view to update when a value changes, mark that value with @State. SwiftUI watches it and redraws the view automatically.
@State var message = "Hello, world!"
Button — A tappable view. The first argument is what to do when tapped; the label is what to show.
Button("Say something new") {
message = "Now we're cooking."
}
VStack — Stack views vertically. Put your Text and Button inside one and they'll appear one above the other.
The full picture
Put it together and you have a view that holds state, displays it, and updates it on demand. That's the core loop of almost every SwiftUI app you'll ever build.
Download and run it
Open the Playgrounds file from GitHub, hit Run, and say hello.