Search code examples
swiftios8

How to load URL in UIWebView in Swift?


I have the following code:

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

I am getting the following error:

'NSURLRequest' is not convertible to UIWebView.

Any idea what the problem is?


Solution

  • 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)