ios 带有 URL 内容的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26532113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:18:34  来源:igfitidea点击:

String With Contents Of URL?

iosswift

提问by imnilly

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?

我很迷惑。在swift中实现以下objective-c方法的正确方法是什么?

NSString * g_home_url = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:home_url] encoding:NSUTF8StringEncoding error:nil];

回答by Guillaume Algis

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

使用-initWithContentsOfURL:encoding:error:实例方法而不是+stringWithContentsOfURL:encoding:error:类便利初始值设定项。

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.

我不知道 Swift 现在是否不支持类便利初始化程序,但这是有道理的,因为它们只是 alloc-init 样板的简写,而 Swift 中不存在。

回答by Niklas Berglund

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

对于 Swift 3,您必须使用String(contentsOf:encoding:). 它抛出。

do {
    var content = try String(contentsOf:URL(string: "http://your-URI-here")!)
}
catch let error {
    // Error handling
}

回答by Sergey Di

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

目前(Swift 4.1.2)从 Ubuntu linux 发送 HTTP-get 请求的唯一方法是: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)
}

回答by Sasi M

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

对于 Swift 4,他们更改了字符串编码。所以现在将是,

do {
  var g_home_url = try String(contentsOfURL: URL.init(string: url_string)!, encoding: String.Encoding.utf8)
}
catch {
   print(error)
}

回答by Manu

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