因此,在我下载最近的更新之前,下面的代码为我工作:
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"
我很困惑。什么是正确的方法来消除以下目标-c方法的快速?
NSString * g_home_url = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:home_url] encoding:NSUTF8StringEncoding error:nil];
发布于 2014-10-23 15:56:52
使用-initWithContentsOfURL:encoding:error:
实例方法,而不是使用+stringWithContentsOfURL:encoding:error:
类方便初始化器。
var g_home_url = String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding, error: nil)
我不知道在Swift中类便利性初始化器是否不受支持,但这是有意义的,因为它们只不过是在Swift中不存在的alloc init样板的缩写。
发布于 2016-12-11 09:07:21
对于Swift 3,您必须使用String(contentsOf:encoding:)
。它扔了。
do {
var content = try String(contentsOf:URL(string: "http://your-URI-here")!)
}
catch let error {
// Error handling
}
发布于 2018-06-07 12:27:00
此时(SWIFT4.1.2)从Ubuntu发送HTTP请求的唯一方法是: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)
}
https://stackoverflow.com/questions/26532113
复制相似问题