How to load URL in UIWebView in Swift?

前端 未结 18 1458
南旧
南旧 2020-11-29 00:58

I have the following code:

UIWebView.loadRequest(NSURLRequest(URL: NSURL(string: \"google.ca\")))

I am getting the following error:

18条回答
  •  孤城傲影
    2020-11-29 00:59

    loadRequest: is an instance method, not a class method. You should be attempting to call this method with an instance of UIWebview as the receiver, not the class itself.

    webviewInstance.loadRequest(NSURLRequest(URL: NSURL(string: "google.ca")!))
    

    However, as @radex correctly points out below, you can also take advantage of currying to call the function like this:

    UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "google.ca")!))   
    

    Swift 5

    webviewInstance.load(NSURLRequest(url: NSURL(string: "google.ca")! as URL) as URLRequest)
    

提交回复
热议问题