Swift Combine: What does objectWillChange do?

What will happen in sink { } block?

objectWillChange

objectWillChange.sink is not showing you the latest value

Sample App
TextField and Button
What would viewModel.text print?
What would happen?

Strange Result

This prints out the previous value.
Instead of “value changed to Hello World”, it’s just “Click to change value”!

Set up ObservableObject without @Published

Set up ObservableObject without @Published

Conclusion

  1. ObservableObject gives you objectWillChange property.
  2. You can access objectWillChange and subscribe.
  3. objectWillChange is called before actually changing the value!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

KD Knowledge Diet

2.4K Followers

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