Home > Enterprise >  Cannot assign to property: 'absoluteString' is a get-only property
Cannot assign to property: 'absoluteString' is a get-only property

Time:07-15

I'm trying to fill the textfield with the current web page url but i'm getting this error

Cannot assign to property: 'absoluteString' is a get-only property

         @ObservedObject var navigationState = NavigationState()

         //Address bar
         TextField("username", text: Binding<String>(
            get: {self.navigationState.url?.absoluteString ?? ""},
            set: {self.navigationState.url?.absoluteString = $0 }))
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .padding(.top, 1.5)
            .onSubmit {
                ............
                }

Navigation State

class NavigationState : NSObject, ObservableObject {
    @Published var url : URL? = nil

    let webView = WKWebView()
    

}

    


extension NavigationState : WKNavigationDelegate {
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        self.url = webView.url
        
    }
}

CodePudding user response:

It should be created URL from string, like

 TextField("username", text: Binding<String>(
    get: {self.navigationState.url?.absoluteString ?? ""},
    set: {self.navigationState.url = URL(string: $0) }))
  • Related