App 14 — 24 Apps You Can Make Today

Choose-Your-Own-Adventure

An interactive story where every tap is a choice — built around an enum-driven state machine.

  • ios
↓ Download Playgrounds File

An interactive story has pages, choices, and consequences. This app builds one — tap a choice and the story branches. Under the hood it's an enum-driven state machine, one of the most useful patterns in all of programming.

What you'll build

A story that starts on a single page and branches based on the player's choices. Each page shows some text and two or three buttons. Tap a button and the story moves forward to a different page.

Key concepts

An enum for story state — Each possible page of the story is a case in an enum. Switching on the current case tells the view what to show.

enum StoryPage {
    case start
    case forestPath
    case caveEntrance
    case treasure
    case goblin
    // ...
}

@State var currentPage: StoryPage = .start

switch in a @ViewBuilder — Wrap a switch on currentPage inside a Group or use it directly inside VStack to show the right page content.

switch currentPage {
case .start:
    StoryTextView(text: "You stand at a crossroads...")
    Button("Take the forest path") { currentPage = .forestPath }
    Button("Descend into the cave") { currentPage = .caveEntrance }
// ...
}

Encapsulating page content — Each case can be its own helper view or a computed property, keeping the switch readable.

State machines everywhere

An enum + switch + @State is a state machine. It's how every game's screens work, how app navigation works, how loading/error/success UI flows work. This story is a toy, but the pattern is everywhere.

Download and run it

Open the Playgrounds file from GitHub, hit Run, and choose your path.

← All apps in this book