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 MVVM


SwiftUI MVVM

Separate UI from business logic by placing state and operations in a ViewModel observed by your SwiftUI views.


Model-View-ViewModel

MVVM separates UI (View) from business logic (ViewModel) and data (Model).

The ViewModel is an ObservableObject the View observes.

Syntax:

  • class VM: ObservableObject { @Published var state }
  • @StateObject private var vm = VM()
  • List(vm.items) { ... }
  • ObservableObject: A class that notifies its observers when its state changes.
  • @Published: A property wrapper that publishes changes to a value, notifying observers.
  • @StateObject: A property wrapper that creates a new instance of a class, making it a source of truth for a view.

Example

import SwiftUI

struct MVVMBasicView: View {
  @StateObject private var vm = TodoViewModel()
  var body: some View {
    List(vm.todos) { t in Text(t.title) }
      .onAppear { vm.load() }
  }
}
import Foundation
import Combine

struct Todo: Identifiable { let id: Int; let title: String }

final class TodoViewModel: ObservableObject {
  @Published var todos: [Todo] = []
  func load() { todos = [Todo(id: 1, title: "Learn MVVM")] }
}
import SwiftUI

struct ContentView: View {
  var body: some View { MVVMBasicView() }
}
import SwiftUI

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

This example shows the MVVM flow: a ViewModel exposes state, the View observes it, and triggers loading on appear.



Sample App: Notes (MVVM)

Example

import SwiftUI

struct NotesMVVMView: View {
  @StateObject private var vm = NotesViewModel()
  var body: some View {
    List(vm.notes) { n in
      VStack(alignment: .leading) {
        Text(n.title).font(.headline)
        Text(n.body).font(.subheadline)
      }
    }
    .onAppear { vm.loadMock() }
  }
}
import Foundation
import Combine

struct Note: Identifiable { let id: UUID; var title: String; var body: String }

final class NotesViewModel: ObservableObject {
  @Published var notes: [Note] = []
  func loadMock() {
    notes = [
      Note(id: UUID(), title: "First", body: "Welcome to Notes"),
      Note(id: UUID(), title: "Second", body: "Try editing...")
    ]
  }
}
import SwiftUI

struct ContentView: View {
  var body: some View { NotesMVVMView() }
}
import SwiftUI

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

This example creates a simple Notes model and ViewModel, then renders a list and loads mock data when the view appears.

Next, connect data to a server in Networking or persist locally in Core Data.


App Group

Use an App Group to share small values (like counts, flags, timestamps) between your app and its Widget.

The hook below writes notesCount whenever the ViewModel's notes change.

How to use it:

  • Enable the same App Group on both the app target and the widget target (e.g., group.com.example.notes).
  • Keep updateSharedNotesCount writing to that group ID; the didSet observer writes the new count on every change.
  • Read the value from the widget using the same suiteName (see snippet below).

App Group hook (Widget Count)

Example

import Combine

func updateSharedNotesCount(_ count: Int) {
  let defaults = UserDefaults(suiteName: "group.com.example.notes")
  defaults?.set(count, forKey: "notesCount")
}

class NotesViewModel: ObservableObject {
  @Published var notes: [Note] = [] {
    didSet { updateSharedNotesCount(notes.count) }
  }
  func add(title: String, body: String) {
    notes.append(.init(id: UUID(), title: title, body: body))
  }
  func delete(at offsets: IndexSet) {
    notes.remove(atOffsets: offsets)
  }
}

Example

import SwiftUI

// Option A: Direct read via UserDefaults (e.g., from a TimelineProvider)
let defaults = UserDefaults(suiteName: "group.com.example.notes")
let count = defaults?.integer(forKey: "notesCount") ?? 0

// Option B: @AppStorage with a custom store (usable in a Widget view)
struct WidgetCountView: View {
  @AppStorage("notesCount", store: UserDefaults(suiteName: "group.com.example.notes"))
  private var notesCount: Int = 0

  var body: some View {
    Text("Notes: \(notesCount)")
  }
}

Tip: Inject dependencies (e.g., networking clients) into your ViewModel to keep views simple and testable.



×

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.