In this video, I am going to show you different implementation when you move @State property from a view to a ObservableObject class.
The advantage of properties in view models is that you can add more business logic with the Combine framework. @Published is important because it will redraw the view whenever the value changes. This is working by calling objectWillChange.send() in the willSet property observer.
But you get a publisher for your property too.
You will see how to create a data stream that saves the property to UserDefaults. UserDefault has some sophisticated mechanism when it actually saves the value. It has some resource management that will save the newest value every 10 sec or so. I am using debounce, to create a similar behaviour. Therefor, in practice you will not need to use debounce with UserDefaults, but I thought it was a nice demonstration. If you switch to save in the file system, this is going to be useful.
⬇️ you can find the image here: [ Ссылка ]
If you want to check out the other videos that I made about property wrappers in SwiftUI you can go here:
Property wrappers in SwiftUI [ Ссылка ]
If you liked what you learned and you want to see more, check out one of my courses!
👨💻 my SwiftUI course [ Ссылка ]
👨💻 my Core Data and SwiftUI course [ Ссылка ]
👩🏻💻 Combine course [ Ссылка ]
#SwiftUI #Xcode #iOS
Ещё видео!