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

SOLVED: Can there be multiple instances of the same EnvironmentObject?

Forums > SwiftUI

Hi there!

My code is working, so I don't really have an issue, but I'm still trying to understand how everything works which is why I'm asking this question.

lets say I have a class like this (made up code so I might have missed some part here and there, it's just to illustrate my issue):

class AnObject:ObservableObject{
        @Published var id: Int = 0
        @Published var name: String = ""

        init(){
        }
}

Then I have a MainView with a tabview where an environmentObject is created to be used in several places like this:

struct MainView: View {
        @StateObject var thisObject = AnObject()

        var body: some View {
                TabView{
                        Tab1View().tabItem {Label("ObjectList",systemImage:"list.dash")}
                        Tab2View().tabItem {Label("OtherView",systemImage:"some.other.icon")}
                }
                .environmentObject(thisObject)
        }
}

And the Tab1View has a list of many AnObject and looks like this:

struct Tab1View:View{
      @EnvironmentObject var thisObject:AnObject()

      var body: some View {
              NavigationView{
                      List(listOfManyAnObjects) {item in
                              NavigationLink (destination: NewView().environmentObject(item)){
                                      Text(item.name)
                              }
                      }
              }
      }
}

NewView looks like this: struct NewView:View{ @EnvironmentObject var thisObject:AnObject

    var body: some View {
            Text(thisObject.name)
    }

}

I have a code similar to this (on another computer, so couldn't copy it) and it shows the correct text, but I'm not entirely sure why. When you click an object in that list and the new view opens up, will the "thisObject" in the NewView be the same EnvironmentObject as everywhere else in the app, thus having replaced any previous object in that variable? Or will it be a new Instance with the same name? Is there a better way for me to set or change the object in "thisObject" while still using the NavigationLink?

3      

The EnvironmentObject should be the same object and not a new instance. You use the same instance in your whole app.

4      

Declare the EnvironmentObject like this: @EnvironmentObject var thisObject and not @EnvironmentObject var thisObject:AnObject().

You could also pass the EnvironmentObject like this: .environmentObject(AnObject())

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.