Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Building & Running SwiftUI Apps in Xcode
Written by Team Kodeco

When you create a new SwiftUI project in Xcode, you start with a simple SwiftUI view. For instance, Xcode might generate a ContentView like the one in our “Hello World” example:

struct ContentView: View {
  var body: some View {
    Text("Hello, world!")
      .padding()
  }
}

To build and run this app, click the Play button in Xcode’s toolbar or press Command-R. This compiles the code and launches the app in the simulator or a connected iOS device, selectable from the drop-down next to the Play button.

Your preview should look like this:

Press Play to build and run. Select the destination to change simulators or test devices.
Press Play to build and run. Select the destination to change simulators or test devices.

Upon a successful build, you should see “Hello, world!” displayed on the device or simulator screen:

Hello World app in an iPhone simulator.
Hello World app in an iPhone simulator.

If you encounter build errors or warnings, Xcode’s debug console provides detailed information. To stop the app, press Command-. or click the Stop button in the toolbar.

This simple process of building and running your app is the first step towards more advanced development tasks with SwiftUI.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.