title: Iterating an array on an @EnvironmentObject when the application closes on macOS
body: <p>I have an <code>@EnvironmentObject</code> that serves an array to my main view. it's declared as follow:</p>
  <p><strong>my_app.swift</strong></p>
  <pre><code>@main
  struct My_AppApp: App {    
      var body: some Scene {
          WindowGroup {
              ContentView()
                  .environmentObject(DataModel())
          }
      }
  }
  </code></pre>
  <p><strong>ContentView.swift</strong></p>
  <pre><code>struct NoteItem: Codable, Hashable, Identifiable {
      let id: UUID
      var text: String
      var date = Date()
      var changed: Bool = false
  }
  
  final class DataModel: ObservableObject {
      @AppStorage(&quot;mytestapp&quot;) public var notes: [NoteItem] = []
      init() {
          self.notes = self.notes.sorted(by: {
              $0.date.compare($1.date) == .orderedDescending
          })
      }    
  }
  </code></pre>
  <p>I call this from the different views in the ContentView.swift as:</p>
  <pre><code>struct AllText: View {
     @EnvironmentObject private var data: DataModel
  }
  </code></pre>
  <p>I added to my_app.swift th ability to detect when the user closes the app so I can perform some action.</p>
  <pre><code>#if os(macOS)
  class AppDelegate: NSObject, NSApplicationDelegate {
      func applicationWillTerminate(_ aNotification: Notification) {        
          // trying to iterate on the struct within DataModel() here
          print(&quot;app closing&quot;)
      }
  }
  #endif
  
  
  @main
  struct My_AppApp: App {
      
      #if os(macOS)
          @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
      #endif
      
      var body: some Scene {
          WindowGroup {
              ContentView()
                  .environmentObject(DataModel())
          }
      }
  }
  </code></pre>
  <p>And now, I'm trying to access the struct within <code>DataModel()</code> so I can check if each element has a changed set but no matter what I try, or how I declare the environmentObject I get a <code>segfault</code>, or errors such as <code>No ObservableObject of type DataModel found. A View.environmentObjectfor DataModel may be missing as an ancestor of this view.</code></p>
  <p>How can I access that DataModel and iterate thru it so I can perform an action when I close the app?</p>