18

So before I downloaded the recent update, the following code worked for me:

var g_home_url = String.stringWithContentsOfURL(NSURL(string: url_string), encoding: NSUTF8StringEncoding, error: nil) // Gives me an error: "String.Type does not have a member names stringWithContentsOfUrl"

I am confused. What is the proper way to acieve the following objective-c method in swift?

NSString * g_home_url = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:home_url] encoding:NSUTF8StringEncoding error:nil];
picciano
  • 22,341
  • 9
  • 69
  • 82
imnilly
  • 317
  • 2
  • 4
  • 10

5 Answers5

21

Use the -initWithContentsOfURL:encoding:error: instance method instead of the +stringWithContentsOfURL:encoding:error: class convenience initializer.

var g_home_url = String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding, error: nil)

I have no idea if class convenience initializers are now unsupported in Swift, but it would make sense as they were just shorthands for the alloc-init boilerplate, which doesn't exist in Swift.

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
  • 1
    `initWithContentsOfURL` and `stringWithContentsOfURL` are mapped to the *same* Swift method, and it seems that only the init method can be called from Swift in such a case. http://stackoverflow.com/questions/26491508/creating-an-object-in-swift-using-the-objective-c-factory-method-gives-a-compile describes a similar issue. – Martin R Oct 23 '14 at 16:11
  • `contentsOfURL` will fetch the response for that url. I don't know what's the reason. I use the quote syntax to convert it now. http://stackoverflow.com/questions/27062454/converting-url-to-string-and-back-again/31586194#31586194 – Roger Jul 23 '15 at 11:46
11

For Swift 3 you'll have to use String(contentsOf:encoding:). It throws.

do {
    var content = try String(contentsOf:URL(string: "http://your-URI-here")!)
}
catch let error {
    // Error handling
}
Niklas Berglund
  • 3,563
  • 4
  • 32
  • 32
1

At this moment (Swift 4.1.2) the only one way to send HTTP-get request from Ubuntu linux is: https://github.com/dmcyk/SwiftyCurl

import Foundation
import SwiftyCurl

var request = cURLRequest(url: URL(string: "https://path.to/url/")!, method: .get)
let connection = cURLConnection(useSSL: true)

do {
  let res = try connection.request(request)
  if let body: String = res.body() {
     print(body)
  }
} catch {
   prin(error)
}
Serhii Didanov
  • 2,200
  • 1
  • 16
  • 31
0

Here what it worked for me: (There is no more error argument)

var g_home_url = try! String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding)

or if you want to handle the error:

do {
  var g_home_url = try String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding)
}
catch {
   print(error)
}
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
Manu
  • 629
  • 7
  • 6
0

For Swift 4 they have changed string encoding. So now it will be,

do {
  var g_home_url = try String(contentsOfURL: URL.init(string: url_string)!, encoding: String.Encoding.utf8)
}
catch {
   print(error)
}
Sasi
  • 1,666
  • 2
  • 24
  • 44
  • `do { let ipAddress = try String(contentsOf: url, encoding: String.Encoding.utf8) } catch { print(error) }` – Ahmed Khedr Apr 27 '18 at 05:48
  • This solution don't on Linux with Swift 4.1.1. String(contentsOf: url) returns nothing – Serhii Didanov May 07 '18 at 12:26
  • @SergeyDi I encounter the same problem... I have also checked https://bugs.swift.org/browse/SR-7620 , but no update yet. Do you have any workaround? – 5t111111 May 10 '18 at 13:22
  • 1
    @5t111111 Only to use swift 3.x. But on 3.x version String(contentsOf also has an issue. Each time you call it, /tmp/someName.tmp file created. – Serhii Didanov May 11 '18 at 10:14