首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >包含URL内容的字符串?

包含URL内容的字符串?
EN

Stack Overflow用户
提问于 2014-10-23 15:50:16
回答 5查看 30.9K关注 0票数 18

因此,在我下载最近的更新之前,下面的代码为我工作:

代码语言:javascript
运行
复制
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方法的快速?

代码语言:javascript
运行
复制
NSString * g_home_url = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:home_url] encoding:NSUTF8StringEncoding error:nil];
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-10-23 15:56:52

使用-initWithContentsOfURL:encoding:error:实例方法,而不是使用+stringWithContentsOfURL:encoding:error:类方便初始化器。

代码语言:javascript
运行
复制
var g_home_url = String(contentsOfURL: NSURL(string: url_string)!, encoding: NSUTF8StringEncoding, error: nil)

我不知道在Swift中类便利性初始化器是否不受支持,但这是有意义的,因为它们只不过是在Swift中不存在的alloc init样板的缩写。

票数 21
EN

Stack Overflow用户

发布于 2016-12-11 09:07:21

对于Swift 3,您必须使用String(contentsOf:encoding:)。它扔了。

代码语言:javascript
运行
复制
do {
    var content = try String(contentsOf:URL(string: "http://your-URI-here")!)
}
catch let error {
    // Error handling
}
票数 11
EN

Stack Overflow用户

发布于 2018-06-07 12:27:00

此时(SWIFT4.1.2)从Ubuntu发送HTTP请求的唯一方法是:https://github.com/dmcyk/SwiftyCurl

代码语言:javascript
运行
复制
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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26532113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档