← All courses

Introduction to SwiftUI

🗓 May 31, 2026 ⏱ 3 min read

What is SwiftUI?

SwiftUI is Apple’s modern framework for building user interfaces, introduced in 2019. With a single codebase written in Swift, you can build apps for iPhone, iPad, Mac, Apple Watch and Apple TV. It is declarative, which is a fundamentally different way of thinking about UI compared to the older UIKit.

Declarative vs imperative — the big idea

This shift is the most important thing to understand:

  • Imperative (UIKit): you create views, then write step-by-step instructions to change them — “find the label, set its text, change its color”. You manage every update by hand.
  • Declarative (SwiftUI): you describe what the UI should look like for the current data. When the data changes, SwiftUI automatically rebuilds the affected parts. You never manually update a view.

In other words: in SwiftUI, your UI is a function of your state. Change the state, and the UI follows automatically. This removes a whole category of bugs where the screen and the data get out of sync.

Your first view

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "globe")
                .font(.largeTitle)
                .foregroundStyle(.purple)
            Text("Hello, SwiftUI!")
                .font(.title2)
        }
        .padding()
    }
}

Every SwiftUI screen is a struct that conforms to the View protocol and provides a body. The body describes what to draw. some View means “some specific kind of view” — you don’t need to spell out the exact type.

Everything is a small, composable View

A View is a lightweight value (a struct) describing a piece of UI. Text, Image, Button — all are views. You combine small views into bigger ones, which keeps code readable and reusable. SwiftUI creates and destroys these structs constantly; they’re cheap, so this is by design.

Modifiers

You customise a view by chaining modifiers like .padding(), .font() and .foregroundStyle(). Each returns a new, modified view (you’ll go deep on these later).

The live Preview

One of SwiftUI’s best features is the live Preview canvas in Xcode. Add a preview and see your UI render instantly as you type — no need to build and run the whole app.

#Preview {
    ContentView()
}

SwiftUI vs UIKit — which to learn?

SwiftUI is the future and is what Apple invests in most. For new apps, start here. UIKit is still everywhere in existing apps, so knowing it helps on real-world teams — but you can be productive in SwiftUI very quickly because it needs so little code.

Common beginner mistakes

  • Trying to “update” a view directly like in UIKit — instead, change the state and let SwiftUI redraw.
  • Forgetting that body can be called many times — keep it free of side effects.
  • Fighting the framework with UIKit habits — embrace the declarative mindset.
Summary: SwiftUI is declarative — your UI is a function of your state. You describe the screen for the current data, and SwiftUI keeps it in sync automatically. Build from small composable views and use the live Preview to iterate fast.