Chaim Friedman
Chaim Friedman

Reputation: 720

Xcode 6 error 'URLWithString' is unavailable?

Please help a newbie here. I'm sure this is stupid simple.

I'm following along and learning Xode nicely, but I'm stumped on a basic connection to a URL:

var text = textField.text
var url = NSURL.URLWithString(text)
var request = NSURLRequest(URL: url)
webView.loadRequest(request)

I'm getting the following error for the second line above:

'URLWithString' is unavailable: use object construction 'NSURL(string:)'

(code is part of a brief tutorial at: http://www.lynda.com/articles/build-first-ios-app-swift)

Upvotes: 21

Views: 9561

Answers (5)

Bek
Bek

Reputation: 2226

Apple changed some of the Swift methods recently, so I've found a few Swift tutorials to be out of date just like what you've encountered. Luckily, it's telling you exactly what to do instead:

Swift 3 update: var url = URL(string:text)

Swift 2: var url = NSURL(string:text)

Upvotes: 24

robertfiorentino
robertfiorentino

Reputation: 594

As of Swift 3.0, the NS prefix is dropped from NSURL:

var url = URL(string: text)

Upvotes: 2

Durul Dalkanat
Durul Dalkanat

Reputation: 7415

func applicationDidFinishLaunching(aNotification: NSNotification?) {

    // Insert code here to initialize your application
    var text = "http://www.google.com"
    var url = NSURL(string:text)
    var req = NSURLRequest(URL: url!)



    webView.mainFrame.loadRequest(req)
}

Upvotes: 9

Malik Brahimi
Malik Brahimi

Reputation: 16711

Some object require explicit argument names unless otherwise defined:

var url = NSURL.URLWithString(string: text)

Upvotes: 1

Guillaume Algis
Guillaume Algis

Reputation: 11016

Use the initializer NSURL(string:):

var url = NSURL(string: text)

Upvotes: 20

Related Questions