UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Got "Cannot find 'objectWillChange' in scope" error that I can't figure out why

Forums > SwiftUI

I'm following along with the instructions here https://www.hackingwithswift.com/books/ios-swiftui/manually-publishing-observableobject-changes

and I'm at the point where I'm supposed to invoke objectWillChange.send() when a variable changes. I understand that part, but I get this error and don't understand why

class FilterModel
{   
    @Published var value = 0 {
        willSet {
            objectWillChange.send()
            print("Hello from willSet")
        }
    }

If I remove the objectWillChange.send() line, I see "Hello from willSet" in my console log every 1.0 seconds.

2      

objectWillChange is a property of the ObservableObject protocol. You have to make FilterModel conform to that protocol in order to get rid of the message.

You also don't need both the @Published property wrapper and the willSet observer. @Published effectively already does behind the scenes what you are doing explicitly here. If you want to do further stuff when value is set, you would use the willSet with objectWillChange.send() but you would need to remove the @Published wrapper.

4      

Thank you! It turns out that some of the problems I was having was actually due to my data source being bad. I think things are working much better now that I'm making progress again :-)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.