Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Swift Basics

Swift HOME Swift Intro Swift Get Started Swift Syntax Swift Statements Swift Output Swift Comments Swift Variables Swift Data Types Swift Type Casting Swift Operators Swift Strings Swift Arrays Swift Ranges Swift If...Else Swift Switch Swift While Loop Swift For Loop Swift Break/Continue Swift Collections

Swift Types & Functions

Swift Functions Swift Optionals Swift Enums & Patterns Swift Closures Tuples & Type Aliases

Swift Object Model

Swift OOP Swift Inheritance Swift Polymorphism Swift Protocols Swift Generics Swift Extensions Access Control Initializers Deinitializers Value Semantics & COW Equatable & Comparable

Swift Robustness & Async

Swift Error Handling Swift Concurrency Swift Memory

Swift Tooling

Swift Package Manager

SwiftUI Basics

SwiftUI Intro iOS Project Setup SwiftUI Layout SwiftUI Navigation SwiftUI Data Flow SwiftUI Lists & Forms SwiftUI Animations SwiftUI Gestures SwiftUI Modifiers & ViewBuilder SwiftUI Previews SwiftUI Accessibility SwiftUI Styling & Theming

SwiftUI Data & Architecture

Networking Persistence Persistence (Core Data) MVVM Architecture AppStorage & SceneStorage Testing SwiftUI

iOS Capabilities

Privacy & Permissions Push Notifications Widgets & Extensions Background Work Core Location App Clips Keychain Basics CloudKit File System Background URLSession MapKit

iOS Quality & Compliance

Localization Accessibility App Privacy In-App Purchases Analytics & Reporting Testing with XCTest

iOS Release & Distribution

Assets & App Icons Signing & Distribution TestFlight & App Store Ship Your First App

Swift Exercises

Swift Exercises Swift Quiz

SwiftUI Introduction


SwiftUI Introduction

Build interfaces declaratively with views, state, and data-driven updates using SwiftUI.


What is SwiftUI?

SwiftUI is Apple's modern UI framework for building apps on iOS, iPadOS, macOS, watchOS, and tvOS.

It is declarative: you describe what the UI should look like, and SwiftUI updates it when your data changes.


Why SwiftUI?

  • Declarative syntax and reactive updates with @State, @Binding, and ObservableObject.
  • Unified API across Apple platforms.
  • Live previews and fast iteration in Xcode.

Building our First "App"

First, you'll build a simple counter view (ContentView) shown by the app entry (App.swift).

Next, you'll refactor the same UI but move its state into a view model (ObservableObject), which scales better in real apps.


Step 1: Basic Example

Syntax: Use @State for local state and bind it to controls (e.g., Button); render with VStack/Text.

Example

import SwiftUI

struct ContentView: View {
  @State private var count = 0

  var body: some View {
    VStack(spacing: 12) {
      Text("Count: \(count)")
      Button("Increment") { count += 1 }
    }
    .padding()
  }
}
import SwiftUI

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

Run Example »

This example shows a simple counter using @State and a button inside a VStack.

About ContentView.swift: In SwiftUI, views live in ordinary .swift files.

The starter view is typically named ContentView.swift, and the app's entry point (App.swift) shows it in a WindowGroup.


Step 2: State with ObservableObject

Use ObservableObject to hold shared state, and @StateObject in the creating view to own its lifecycle.

Views update automatically when @Published properties change.

Syntax: class VM: ObservableObject { @Published var count }, @StateObject private var vm = VM(), bind UI to vm.count.

This example refactors the basic counter above.

The UI and behavior are the same (a counter label and button), but the state now lives in a CounterModel view model managed by @StateObject. This pattern scales better as your app grows.

Example

import SwiftUI
import Combine

class CounterModel: ObservableObject {
  @Published var count = 0
  func increment() { count += 1 }
}
import SwiftUI

struct ContentView: View {
  @StateObject private var model = CounterModel()

  var body: some View {
    VStack(spacing: 12) {
      Text("Count: \(model.count)")
      Button("Increment") { model.increment() }
    }
    .padding()
  }
}
import SwiftUI

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

Run Example »

This example promotes state into a ViewModel and updates the UI via @Published changes observed by the view.

Platform look: SwiftUI adapts controls to the platform. On iOS the button appears as blue text; on macOS it's a bordered push button. The behavior is the same.

See iOS style in Xcode: run an iOS App target (iPhone simulator). If you're in a macOS App and want a text-style button, add .buttonStyle(.plain) to the button.



Run Examples in Xcode

Standard workflow (used in this tutorial)

Every example is organized into three files so you can run it as a small app:

  1. Demo.swift: The example's main code (view and/or supporting types).
  2. ContentView.swift: Shows the demo (references types from Demo.swift).
  3. App.swift: Stable app entry with WindowGroup { ContentView() }.

Tip: In your own Xcode project, keep a single ContentView. If you already have one from a previous example, reuse that file and update its body for the new demo. Alternatively, create a chooser menu and navigate to uniquely named demo views.

Optional: Canvas Preview

You can also run any example via Xcode's Canvas Preview without changing your app entry point.

  1. Create a View file: In Xcode choose File > New > File... > SwiftUI View. Name it (for example, FrameDemo.swift) and paste the example view's code into it.
  2. Add a Preview: Ensure the file has a preview. Use either a PreviewProvider or the #Preview macro.
import SwiftUI
  
  struct FrameDemo_Previews: PreviewProvider {
    static var previews: some View { FrameDemo() }
  }
import SwiftUI
  
  #Preview { FrameDemo() }
  1. Open the Canvas: With the file selected, choose Editor > Canvas (or click the Canvas button). If you don't see the preview, build the project once.
  2. Run the Preview: Click Resume / Play in the Canvas. Edits to the code will refresh the preview. Use the device picker to switch iPhone/iPad models.
  3. Interact: Previews are interactive—tap buttons, type into fields, etc.

Troubleshooting: If the Canvas won't load, try a project Build, then Clean Build Folder. Make sure the target platform is iOS and the file imports SwiftUI.

Run on Simulator: You can also run any example on the simulator by temporarily setting WindowGroup { ExampleView() } in App.swift, or by navigating to the example from a simple in-app menu.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.