Swift Combine: About @Published Wrapper, things you probably didn’t know

KD Knowledge Diet
2 min readAug 10, 2022

If you started developing ios with SwiftUI, you often use it without knowing what @Published is. If you’ve started developing based on UIKit, you’re probably familiar with it. This article is for SwiftUI Developers who barely know about UIKit. But rest assured, even for UIKit Developers, there would be something you’ve never known.

@Published is Combine Publisher

@Published is a property wrapper that makes any property using it Publisher.

@Publisehd is only used in class

Property with @Published is declared on ViewModel Class

Property Wrapper @Publisehd can only be used for class.

@Published can be used without ObservableObject

Many new ios developers who started developing in SwiftUI think that @Published property wrapper should be used in conjunction with ObservableObject. That’s not true!

@Published is quite similar to CurrentValueSubject

@Published vs CurrenetValueSubject

@Published is quite similar to CurrentValueSubject. Both properties @Published and CurrentValueSubject can hold a value and they can also subscribe. I like to think of @Published as a syntax sugar for CurrentValueSubject.

Difference between @Published and CurrentValueSubject

Difference between @Published and CurrentValueSubject

Whenever you want to change a value, CurrentValueSubject requires to use send() method or you need to access .value property to change its value. But @Published property allows you to change its value.

Conclusion

  • @Published is Combine Publisher
  • @Published can only be used in Class
  • @Published is quite similar to CurrentValueSubject because it can hold a value.
  • @Published doesn’t need to call send() method or access .value. When you directly change its value, it will update the value.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!