Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 46 ยท Getting Started

Xcode and Your First SwiftUI App

Install Xcode, create a SwiftUI project, understand its files, use previews and the simulator, and learn what Info.plist and the asset catalog do.

Xcode is Apple's development environment: editor, compiler, interface previews, debugger, simulator and release tools in one app. Every iOS app is built with it, so your first job is to feel at home in it. This lesson walks through installing Xcode, creating a SwiftUI project, reading the files it generates, and running the result.

Everything here happens on a Mac, so the code blocks are read-only. Two small stand-ins at the end model ideas you will meet in the project, so you can check your understanding in the browser.

Installing Xcode

Xcode is a free download from the Mac App Store, and also available from Apple's developer site. It is large (several gigabytes, and more once you add simulators), so make sure you have free disk space. You need a Mac whose macOS version is new enough for the Xcode release; each Xcode version lists its minimum macOS.

When you open it for the first time, Xcode may offer to install additional components, such as the iOS platform and its simulator runtime. Accept them. Later you can add or remove simulator runtimes under the Settings window in the Components area.

You do not need a paid developer account to start. A free Apple ID lets you run apps in the simulator and, with some limits, on your own device. The paid Apple Developer Program is only required to distribute through TestFlight and the App Store.

Creating a project

Choose File > New > Project, pick the iOS tab and the App template, then fill in the options:

  • Product Name: the name of your app, for example Greeter.
  • Team: your Apple ID (or "None" for now; the simulator does not need one).
  • Organization Identifier: a reverse-domain string such as com.example. Together with the product name it forms the bundle identifier (com.example.Greeter), the unique ID of your app.
  • Interface: SwiftUI.
  • Language: Swift.
  • Testing and storage options: leave the defaults (or untick them to keep the project small).

Xcode then creates a folder with a .xcodeproj project file and your source files.

The project structure

A new SwiftUI project contains a handful of files. The names can vary slightly between Xcode versions, but you will see this shape:

TEXT
Greeter/
  Greeter.xcodeproj
  Greeter/
    GreeterApp.swift      the app entry point (@main)
    ContentView.swift     the first screen
    Assets.xcassets       images, colors and the app icon
    Preview Content/      sample data for previews (older templates)
  GreeterTests/           unit tests
  GreeterUITests/         UI tests

The two Swift files matter most. GreeterApp.swift is where the program starts:

Swift
import SwiftUI

@main
struct GreeterApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

@main marks the entry point. The App protocol describes your app as a collection of scenes; a WindowGroup is a scene that shows your root view in a window (on iPhone, in the full screen). ContentView is just a view, defined in the other file:

Swift
import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
            Text("Hello, world!")
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

Do not worry about every keyword yet. The next lesson explains View and body. What matters now is that the file ends with #Preview, which tells Xcode how to show this view live in the editor.

Four files to know

App file @main starts the program.
ContentView Your first screen.
Assets Icons, images, colors.
Info.plist Settings the system reads.
1 / 2

Previews, the simulator and real devices

Previews render a view next to your code, and refresh when you edit. You can show several versions at once, for example light and dark mode, or a large text size:

Swift
#Preview("Dark, large text") {
    ContentView()
        .preferredColorScheme(.dark)
        .dynamicTypeSize(.xxxLarge)
}

Previews are for fast feedback while designing. They are not a complete runtime: things like push notifications, real networking conditions or long-running state are better tested elsewhere.

The simulator runs a full virtual iPhone or iPad on your Mac. Pick a device in the toolbar, then press Run (the play button, or Command-R). The simulator is excellent for layout and flow across screen sizes. It does not have a real camera, cannot produce real push tokens in all cases, and its performance does not match a phone.

A real device shows true performance, touch feel, camera, sensors and battery behavior. Connect an iPhone with a cable (or pair it over Wi-Fi), enable Developer Mode on the phone, select it in the toolbar and run. You must sign the app with a team; a free Apple ID works for personal testing, with limits on how long the installed build stays valid.

Rule of thumb: design with previews, check flows in the simulator, and always do a final pass on a real device.

Info.plist, capabilities and assets

iOS reads an app's configuration from an Info.plist (property list) file. Modern Xcode projects hide most of it in the target's Info tab, but the keys are the same: the display name, the bundle version, supported orientations, and usage descriptions. A usage description is the text shown in the permission dialog:

XML
<key>NSCameraUsageDescription</key>
<string>The camera scans receipts so you do not have to type them.</string>

If your app touches the camera, photos, location, microphone and similar, and you forget the matching key, the system terminates the app when it asks for access. Write descriptions that tell the user why, in plain language.

Capabilities (the Signing & Capabilities tab) switch on platform features such as push notifications, iCloud or Sign in with Apple. Some of them change the app's entitlements, a signed list of what the app may do.

The asset catalog, Assets.xcassets, stores images, named colors and the app icon. Items are looked up by name, so renaming an asset breaks code that refers to it. Catalogs also hold variants: a color can have light and dark values, and an image can provide different versions per scale.

The two stand-ins below model what the system does with these files.

Swift Stand-in: reading Info.plist keys

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Swift Stand-in: a preview matrix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readLine()), it gets the next line from this box.

Each #Preview you write is one entry in a matrix like this one: the same view, shown under different conditions. Checking a few of them early catches clipped text and unreadable colors long before release.

Build errors and where to look

When something fails, open the Issue navigator (the warning triangle in the left sidebar) to see all errors and warnings, then click one to jump to the line. Fix the first error first; later ones are often consequences. If the project behaves oddly after big changes, Product > Clean Build Folder forces a fresh build. We cover debugging in depth in Debugging Xcode Apps.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the @main attribute on the App type do?

    @main tells the compiler which type provides the program's entry point. The App's scenes then show the UI.
  2. 2. What happens if your app asks for camera access but Info.plist has no NSCameraUsageDescription?

    Usage descriptions are mandatory for protected resources. Without one, the system ends the app at the moment of the request.
  3. 3. Which tool is best for a final check of performance and touch feel?

    Previews and the simulator are convenient, but only a real device shows true performance, sensors and battery behavior.
  4. 4. What forms the bundle identifier of a new project?

    For example, com.example plus Greeter gives com.example.Greeter, the unique ID of the app.

Summary

  • Xcode is Apple's all-in-one IDE; it needs a Mac, and the free version is enough to start.
  • The App template gives you an @main app type with a WindowGroup and a ContentView.
  • #Preview shows views live in the editor; use several previews for dark mode and large text.
  • The simulator is great for layouts and flows; always finish on a real device.
  • Info.plist holds configuration and permission descriptions; capabilities add platform features.
  • The asset catalog stores images, named colors and the app icon, looked up by name.

Next: The SwiftUI Mental Model