iOS Swift NSURL 网址无效
iOS Swift NSURL Web Address Not Working
我正在尝试从服务器获取数据。我的 URL 是(例如):http://dommain.com/action
当我执行
let nsURL = NSURL(fileURLWithPath: url)
URL 被翻译成 http:/domain.com/action。然后当我执行这个时:
let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {
(data, response, error) in
print("data=\(data) response=\(response) error=\(error)")
}
错误 属性 表示找不到“file:///http:/domain.com/action
我不确定我做错了什么。
替换
let nsURL = NSURL(fileURLWithPath: url)
和
let nsURL = NSURL(string: url)
An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.
NSURL(fileURLWithPath: url)
初始化并 returns 新创建的 NSURL 对象作为具有指定路径的 file URL。对于网络 URL,请改用 NSURL(string: url)
。
对于 Swift 3 及以上,使用 URL 而不是 NSURL
:
let u = URL(string: url)
我正在尝试从服务器获取数据。我的 URL 是(例如):http://dommain.com/action
当我执行
let nsURL = NSURL(fileURLWithPath: url)
URL 被翻译成 http:/domain.com/action。然后当我执行这个时:
let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL) {
(data, response, error) in
print("data=\(data) response=\(response) error=\(error)")
}
错误 属性 表示找不到“file:///http:/domain.com/action
我不确定我做错了什么。
替换
let nsURL = NSURL(fileURLWithPath: url)
和
let nsURL = NSURL(string: url)
An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data.
NSURL(fileURLWithPath: url)
初始化并 returns 新创建的 NSURL 对象作为具有指定路径的 file URL。对于网络 URL,请改用 NSURL(string: url)
。
对于 Swift 3 及以上,使用 URL 而不是 NSURL
:
let u = URL(string: url)