DevPrep

All topics › Combine

What does @Published do?

Middle PRO

The essence

@Published is a property wrapper that adds a Publisher to a class property: $property has type Published<Value>.Publisher with Failure == Never. It emits the new value on willSet — before the property has actually changed. It works in any class (not in a struct: the wrapper holds its inner publisher by reference), not only in an ObservableObject. ObservableObject merely adds the synthesized objectWillChange on top: the compiler forwards events from all @Published properties into it, and SwiftUI re-renders views on that.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store