MacOS + Xcode + Swift + Observable Object : Xcode says I need to include arguments when creating observable object class

Hello,
I am new to Swift. I am attempting to create an initial view with a Navigation pane that includes a Navigation Link. After I select the Navigation Link I can display a different view but am unable to redraw the navigation pane and its navigation link with new button text and destination. It is my understanding that I could create a state variable in the initial view and when its value changed the navigation panel would be redrawn. Unfortunately I could never figure out how to change the state variable in the view.
So I thought I could use an observable object and set it in each view to display the desired Navigation Link. Unfortunately my class creation line fails. Xcode thinks I need to add arguments. I have no idea what arguments it is referring to. Any help in pointing me in the right direction will be appreciated.
Below is my code:

Chris

localObject.firstPass = true is not permitted in the top-level of a type. Consider putting this code in the initializer:

struct ContentView: View {

    @ObservedObject var localObject = ObjectToBeObserved()

    init() {
        localObject.firstPass = true
    }

    // the rest can be the same as in your post above

}

Regarding firstPass - I can see that it is already initialised to true in the ObjectToBeObserved.init(), so you don't need any extra code in ContentView.

But I also see another problem.

@ObservedObject is for objects that are passed from the outside, and owned by something else - often @StateObject, another ObservableObject or some UIKit entity owning HostingController.

Since there are no other views in question, I guess you want ContentView to own ObjectToBeObserved, so you should use @StateObject instead of @ObservedObject.

struct ContentView: View {
    @StateObject var localObject = ObjectToBeObserved()
    var body: some View {
        ...
        ChildView(localObject: localObject)
        ...
    }
}

/// Just to give an example of correct interaction between @StateObject and @ObservedObject
struct ChildView: View {
    @ObservedObject var localObject: ObjectToBeObserved // No initializer!!!
}

If you use @ObservedObject incorrectly, your instance of ObservableObject may be reset unexpectedly. In the following example ChildObject is reset every time when On/Off button is clicked:

struct Parent: View {
    @State var x: Bool = true
    var body: some View {
        VStack {
            Button(x ? "On" : "Off") {
                x.toggle()
            }
            Child()
        }
    }
}

struct Child: View {
    @ObservedObject var object = ChildObject() // Don't do this
    var body: some View {
        Button("\(object.x)") {
            object.x += 1
        }
    }

}

class ChildObject: ObservableObject {
    @Published var x: Int = 0
}

Thank you for the responses. I have updated my code with @stateobject, @observedobject and pass the localobject to the child view and initialize it as you indicated. This solved the compile problem.
I would now like to toggle firstPass every time I change views such that the Navigation Pane redraws with the updated button text and destination in the Navigation Link. In many locations in the code Xcode does not like the statement "localObject.firstPass.toggle()". It would seem logical to place it on some type of onClick action so that it only occurs after I click a button but do not find such an option.

Any suggestions would be appreciated.
Below is my code for the class, parent view (current view) and child view (next current view).

Chris

Hello,

After much research and some great input regarding watching Standord CS193p I better understand how Swift works. Turns out I was going the long away around the barn. I rewrote the code and put the Navigation Links into a List and then created three separate view structures that I can call. In addition, I used the tag and selector features of the Navigation Link to create an initial view. Below is the new code. I did not include the 3 structures called for they are just the default syntax for a view as created by Xcode.

Chris

Terms of Service

Privacy Policy

Cookie Policy