Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of SwiftUI:
A Tech Blog Series

Welcome to our blog series focused on the exciting world of SwiftUI! Here, we aim to share valuable insights, practical tips, and deep dives into the latest SwiftUI features and techniques. From crafting seamless user interfaces to exploring performance strategies, our articles are designed to inspire and empower developers of all levels. Stay connected as we explore the endless possibilities with SwiftUI and spark engaging discussions about building modern, efficient apps.

Explore all SwiftUI Topics

State Management in SwiftUI: @State, @Binding, @ObservedObject, and more

3/2/2025

0 Comments

 
Picture
So far, we’ve built beautiful layouts, but how do we manage dynamic data in SwiftUI? That’s where State Management comes in!

​In this post, you’ll learn:
  • The difference between @State, @Binding, @ObservedObject, @EnvironmentObject, and @StateObject.
  • When to use each property wrapper.
  • How to share data between views effectively.

By the end, you’ll confidently manage state in SwiftUI like a pro. 🚀​

Why Does State Matter?

In SwiftUI, state is what keeps your UI in sync with your data.
  • Without state management → UI won’t update when data changes.
  • With proper state management → Views react dynamically to user actions.

Let’s explore the different ways to manage state in SwiftUI.

1. @State — Simple, Local State

Use @State for small, private variables that belong to a single view.

​Example: Counter App

    
Key Takeaways:
  • Use @State for simple values (e.g., numbers, booleans, strings).
  • It should not be used for complex objects or shared data.
  • SwiftUI automatically updates the UI when the @State value changes.​

​2. @Binding — Passing State to Child Views

If a child view needs to modify a @State variable, use @Binding.

​Example: Toggle Button

    
Key Takeaways:
  • Use @Binding when a child view needs to modify a parent’s state.
  • The @State variable in ParentView remains the single source of truth.​

3. @StateObject — Managing Complex Objects

If a view owns an object and should initialize it only once, use @StateObject.

Example: Timer App

    
Key Takeaways:
  • Use @StateObject for new instances of ObservableObject.
  • SwiftUI ensures the object persists correctly during view updates.
  • Best for managing side effects like API calls, timers, or large datasets.​

4. @ObservedObject — Sharing Objects Between Views

​If a parent creates an object and wants to share it with multiple child views, use @ObservedObject.

​Example: Sharing Data Across Views

    
Key Takeaways:
  • Use @ObservedObject when a parent creates the object and shares it with children.
  • Changes in @Published properties trigger UI updates.
  • Best for passing shared data between views.​


5. @EnvironmentObject — Global Shared State

​For app-wide state, use @EnvironmentObject.

​Example: User Authentication

    
  • Use @EnvironmentObject for app-wide shared data.
  • No need to pass data manually through multiple views.
  • Best for user settings, authentication, and app-wide preferences.​

When to Use Which Property Wrapper?

  • @State — Local state for simple values.
  • @Binding — Passing state to child views.
  • @StateObject — Owning an object in a view.
  • @ObservedObject — Sharing an object between views.
  • @EnvironmentObject — Global state for the entire app.

What’s Next?

​In Week 10, we’ll explore MVVM in SwiftUI — learning how to separate UI from business logic for scalability and testability.
0 Comments
<<Previous
    Picture

    ​Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.

    Archives

    March 2025
    February 2025
    January 2025

    Categories

    All
    Alignment
    Binding
    EnvironmentObject
    Frames
    Grids
    Layout
    List
    Modifiers
    ObservedObject
    Overlay
    Previews
    Responsive Design
    Stacks
    State
    State Management
    StateObject
    Task Manager
    Views
    ZStack

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk