title: How to use edit an environment object from a class function?
body: <p>I am new to swift programming. I was trying to play around with the environment object to update views by changing the value of the environment object specifically via a class function. 
  I have two subviews in the contentview as I wish to see if both subviews get updated together when the string gets changed in UserData environment object.</p>
  
  <p>However, there's an error saying 'Thread 1: Fatal error: No ObservableObject of type UserData found. A View.environmentObject(_:) for UserData may be missing ...'</p>
  
  <p>In SceneDelagate.swift, I have injected the environment object:</p>
  
  <pre><code>let userData = UserData()
  
  window.rootViewController = UIHostingController(rootView:contentView.environmentObject(userData))
  </code></pre>
  
  <p>UserData.swift</p>
  
  <pre><code>final class UserData: ObservableObject {
  
      @Published var image: UIImage? = nil
      @Published var string  = "" 
  
  
  
  }
  </code></pre>
  
  <p>StringGenerator.swift</p>
  
  <pre><code>class StringGenerator {
  
      @EnvironmentObject var userData: UserData
  
  
      func changeURL(){
          print("Changing string now from \(userData.string)")
          if self.userData.string == "HELLO"{
              self.userData.string = "BYE"
  
          }
          else{
              self.userData.string = "HELLO"
          }
          print("to \(userData.string)")
      }
  }
  </code></pre>
  
  <p>SubView1.swift</p>
  
  <pre><code>struct SubView1: View {
      @EnvironmentObject var userData: UserData
  
  
  
      var body: some View {
          Text("\(userData.string)")
  
      }
  }
  </code></pre>
  
  <p>SubView2.swift</p>
  
  <pre><code>struct SubView2: View {
      @EnvironmentObject var userData: UserData
  
  
  
      var body: some View {
          Text("\(userData.string)")
  
      }
  }
  </code></pre>
  
  <p>ContentView.swift</p>
  
  <pre><code>struct ContentView: View {
      @EnvironmentObject var userData: UserData
  
      let stringGenerator = StringGenerator()
  
      var body: some View {
  
          VStack{
  
              Button(action: {
                  print("Changing Text Now !")
                  self.stringGenerator.changeURL()
                  print("\(self.userData.string)")
              }) {
                  Text("Change Text ")
              }
  
  
              SubView1()
              SubView2()
  
  
          }
  
  
      }
  }
  </code></pre>
  
  <p>In general, I wish to know how does one change the environment object via a class function.</p>
  
  <p>The code crashes when I run it. What's the problem in here?</p>