简体   繁体   English

无法获取URL的内容

[英]Cannot get contents of URL

So I have the following URL: 所以我有以下URL:

let theURL : NSURL = NSURL(string: "http://www.iana.org/domains/example")!

But all I get after running: 但是我跑步后得到的一切:

let blob : String
do {
    blob = try String(contentsOfURL: theURL)
}
catch {...}

Is a console statement that reads as follows: 是一个控制台语句,内容如下:

CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme CFURLCopyResourcePropertyForKey失败,因为它传递了没有方案的URL

The URL is valid, and the code works with: 该URL有效,并且该代码可用于:

http://www.example.com http://www.example.com

What am I doing wrong? 我究竟做错了什么?

Here's a cleaner version of what you're trying to do. 这是您要执行的操作的更简洁的版本。

let theURL = NSURL(string: "http://www.iana.org/domains/example")!

let blob : String

do {
  blob = String(theURL)
}
catch { }

Corrections: 更正:

  1. theURL is an optional that was still wrapped that's why you couldn't assign it. theURL是一个仍被包装的可选属性,这就是为什么您无法分配它的原因。
  2. You misused contentsOfURL 您滥用contentsOfURL
  3. If you're immediately assigning the variable you don't need to declare it the way you did 如果您立即分配变量,则无需像以前一样声明它

I'm not sure what you're trying to accomplish, but if you provide me with more information I can better answer your question. 我不确定您要完成什么,但是如果您向我提供更多信息,我会更好地回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM