title: How to retrieve value from Class to textfield Swiftui
body: <p>This is a web browser app. I want to get the url of the current web page loaded, 'urlObservation', from the init and update the textfield of the url address bar with that string. But i'm not sure how to do it.</p>
  <p>WebView</p>
  <pre><code>struct WebView: NSViewRepresentable {
   .........
      class Coordinator: NSObject, WKUIDelegate, WKNavigationDelegate,ObservableObject {
              private var viewModel: WebStateModel
              private var wkWebview = WKWebView()
              var urlObservation: NSKeyValueObservation?
        
  
          init(_ viewModel: WebStateModel, _ wkWebview: WKWebView) {
                 self.viewModel = viewModel
                  self.wkWebview = WKWebView()
                  self.urlObservation = wkWebview.observe(\.url, changeHandler: { (wkWebview, change) in
                              print((wkWebview.url?.absoluteString ?? &quot;Empty&quot;))
                          })
              
                 }
  ......
  }
  </code></pre>
  <p>ContentView</p>
  <pre><code>@State var webView: WebView?
  @State var text = &quot;&quot;
  
  .........
  
     //Address Bar
     TextField(&quot;Enter a URL&quot;, text: Binding(   
                      get: { text },
                      set: { text = WebStateModel.stripHttps($0) } ), onCommit: {
                          webModel.updateUrl(text)
                      })
  
      
  </code></pre>
  <p>Update</p>
  <p>I've tried adding self.text inside the changeHandler to retrieve the current url change but it returns nothing</p>
  <pre><code>   class Coordinator: NSObject, WKUIDelegate, WKNavigationDelegate,ObservableObject {
          private var viewModel: WebStateModel
          private var wkWebview = WKWebView()
          var urlObservation: NSKeyValueObservation?
          @Published var text = &quot;&quot;
         
      init(_ viewModel: WebStateModel, _ wkWebview: WKWebView) {
            
              self.viewModel = viewModel
           
              self.wkWebview = WKWebView()
              super.init()
              self.urlObservation = wkWebview.observe(\.url, changeHandler: {  (wkWebview, change) in
                          print((wkWebview.url?.absoluteString ?? &quot;Empty&quot;))
                          self.text = ((wkWebview.url?.absoluteString ?? &quot;Empty&quot;))
                      })
             }
      
      
  </code></pre>