title: How to Access the NSView reference of a SwiftUI View
body: <p>I have an objective-c code that will take a pointer to NSView and will attach CAMetalLayer to it and pipe video data. I already implemented this on using StoryBoard and a ViewController and an it works fine.</p>
  <p>However, I would like to use SwiftUI if possible. but couldn't find any example that's trying to achieve this.</p>
  <p>Here's a sample code on how I'm trying to grab the NSView of the SwiftUI View</p>
  <pre><code>struct ContentView: View {
  init() {
      print(&quot;init&quot;)
  }
  
  
  var body: some View {
      EmptyView()
          .onAppear {
              DispatchQueue.main.async {
                  print(&quot;window count: &quot;, NSApplication.shared.windows.count)
                  if let window = NSApplication.shared.windows.first {
                      let view = window.contentViewController?.view
                      //pass the view to an obj-c code
                      
                      
                  }
                  
              }
          }
          .frame(maxWidth: .infinity, maxHeight: .infinity)
          .background(Color.red)
          .edgesIgnoringSafeArea(.all)
  
  }
  </code></pre>
  <p>}</p>
  <p>Is this possible with SwiftUI? If so, how can I grab the NSView reference to the underlying SwiftUI View?</p>
  <p>Thanks for reading.</p>