Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 6 error 'URLWithString' is unavailable?

Tags:

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)

like image 234
Chaim Friedman Avatar asked Jan 21 '15 15:01

Chaim Friedman


2 Answers

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)

like image 58
Bek Avatar answered Oct 28 '22 14:10

Bek


Use the initializer NSURL(string:):

var url = NSURL(string: text) 
like image 30
Guillaume Algis Avatar answered Oct 28 '22 15:10

Guillaume Algis