16

I have latest xcode beta, just trying to load a webpage inside an app.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = NSURL (string: "https://www.google.com");
        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

On this line: let requestObj = NSURLRequest(URL: url!); I get following error:

"NSURL" is not implicitly convertible to "URL"; did you mean to use "as" to explicitly convert?

I removed the exclamation mark and I get this error:

Cannot convert value of type "NSURL?" to expected argument type "URL"

I've been trying variations on this code from all over the internet for the past four hours. I'm thinking latest swift just doesn't allow this anymore or something, because nothing works. I'm set to a 10.0 Deployment Target and a Xcode 8.0 compatible. But I've also tried earlier versions for both of those settings and I get the same error across all versions of both.

Yes, my webView on the storyboard is properly connected to the ViewController.swift.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Works for a Living
  • 1,262
  • 2
  • 19
  • 44
  • 1
    In Swift 3, use URL and URLRequest (drop the NS prefix). See http://stackoverflow.com/a/37812485/2227743. – Eric Aya Aug 13 '16 at 10:49
  • 1
    `NSURL (string:)` return an Optional, you have to unwrap it. Cast as URL will work. – Khundragpan Aug 13 '16 at 10:51
  • @K.Nimo OP is already force unwrapping it - and it's better to use the new Swift 3 structs anyway, for URL *and* for URLRequest. :) – Eric Aya Aug 13 '16 at 10:56
  • 1
    No it doesn't. I easily did try that before asking the question and it did NOT fix it for me. It actually created build errors when I tried that. Also, why do you assume everyone asking questions here (noob's like me) are even aware of this feature? Show some decorum. I found out about it from another question here, and it didn't fix my code and it created more build errors. Then I gave up and asked the question. – Works for a Living Aug 13 '16 at 18:27
  • @Thom: I am sorry If I did you wrong, but I tried it myself before posting that comment. I copied your exact code into an Xcode 8 beta 5 iOS project and chose "Edit > Convert > To Current Swift Syntax ..." . The code was updated to `let url = URL (string: "https://www.google.com"); let requestObj = URLRequest(url: url!); `. – Martin R Aug 14 '16 at 08:35
  • Cool. When I did it created build errors. Just be less abrasive to the next guy you think deserves your condescension. It helps to make debugging less stressful and keeps stack overflow a positive environment. ;) All the other comments were pretty friendly. – Works for a Living Aug 14 '16 at 09:44

2 Answers2

22

In swift 3 you need to use URL instead of NSURL, so create url object like this.

if let url = URL(string: "https://www.google.com") {
    webView.load(URLRequest(url: url))
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
4

Update for Swift 3.1:

let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webView.load(request)

Notice the use of URL instead of NSURL and load instead of loadRequest.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Andres
  • 1,090
  • 14
  • 30