Swift’s @Published – The Feature That Makes Your App Respond Instantly

@Published works exactly like that.

It turns your Swift variables into self-updating, auto-notifying values — like having a friend who instantly updates everyone in the group whenever something changes.

Whether you are a:

  • iOS student in college

  • Fresher preparing for interviews

  • Developer building SwiftUI apps

  • Professional exploring clean MVVM architecture

…this feature will make your life easier.

Let’s understand it the Indian way — simple, clear, relatable.


What is @Published in Swift?

@Published is a property wrapper introduced in Swift 5 (Combine framework).
It makes your variable observable, meaning whenever the value changes, Swift automatically informs all the parts of your app that depend on it.

No need for:

  • Notifications

  • KVO

  • Delegates

  • Manual UI updates

Just mark your variable with @Published and you’re done.

Example:

class Counter: ObservableObject {
    @Published var value: Int = 0
}

Whenever value changes → UI automatically updates.


📱 Think of It Like “A WhatsApp Broadcast List”

Imagine you have a WhatsApp broadcast:

  • You send one message

  • Everyone receives it instantly

@Published works the same.

One variable changes →
All subscribers (views, UI elements, listeners) are updated instantly.


🎯 Where Do We Use @Published?

Mostly in SwiftUI + MVVM architecture.

The ViewModel properties that change UI should be marked as @Published.

Example:

class LoginViewModel: ObservableObject {
    @Published var email = ""
    @Published var password = ""
}

In SwiftUI:

Button("Login") {}
    .disabled(viewModel.email.isEmpty || viewModel.password.isEmpty)

As the user types → button enables automatically.
No extra code. No manual updating.

This is why @Published is so popular among Indian SwiftUI developers.


🎓 Simple Indian Example – A Train Ticket App

Imagine you're making a view model for booking a train ticket:

class TrainBookingViewModel: ObservableObject {
    @Published var passengerName = ""
    @Published var age = 0
    @Published var selectedTrain = ""
}

In UI:

  • When name updates → show preview

  • When train updates → show seat availability

  • When age updates → show concession

All automatically!

No extra functions, observers, or manual reloads.


⚙️ What Happens Behind the Scenes?

When you mark a variable as @Published:

  1. Swift creates a publisher for that variable

  2. Whenever the value changes → an event is broadcast

  3. SwiftUI listens to these events

  4. UI refreshes automatically

This is the engine that powers reactive apps in India like:

  • Ola

  • Zomato

  • Swiggy

  • Paytm

  • Blinkit

Everything updates instantly — and you expect that from modern apps.


Using @Published in Combine (UIKit projects)

Even if you are not using SwiftUI, it's still useful.

viewModel.$passengerName
    .sink { updatedName in
        print("Name changed:", updatedName)
    }
    .store(in: &cancellables)

Whenever the name changes, this subscriber gets notified.
Perfect for UIKit + Combine apps.


🚫 When NOT to Use @Published

Avoid it for:

❌ Huge arrays changing rapidly
❌ Background data streams
❌ When performance is critical
❌ Inside structs (it works with class + ObservableObject)

Use it for:

✔ Form fields
✔ Screen UI states
✔ Toggles, buttons, switches
✔ Login, signup, profile screens
✔ Loading indicators

Basically, anything that should instantly update the UI.


🌟 Why Developers Love @Published

Because it:

  • Removes unnecessary boilerplate

  • Makes your app feel modern and fast

  • Works beautifully with SwiftUI

  • Simplifies MVVM

  • Reduces bugs

  • Is easy to learn

  • Saves development time (super important in Indian projects!)

Most importantly —

👉 It makes your UI react automatically, just like WhatsApp’s “typing…” indicator.


🏁 Conclusion: @Published Makes Your App Feel Alive

Whether you're building:

  • A startup app

  • A college project

  • A company product

  • A personal hobby app

@Published will make your data and UI work together smoothly.

If you want your app to feel modern, intelligent, and reactive —
@Published is one of the most important tools you should master.


Comments

Popular posts from this blog

Complete iOS Developer Guide - Swift 5 by @hiren_syl |  You Must Have To Know 😎 

Debugging

Swift Fundamentals You Can’t Miss! A List by @hiren_syl