简体   繁体   English

NSURLErrorDomain的代码= -1100

[英]NSURLErrorDomain with code=-1100

I was trying to download a picture to my app from 我试图将图片从下载到我的应用中

在此处输入图片说明 The request failed with the error NSURLErrorDomain and the code is really -1100. 请求失败,错误为NSURLErrorDomain,代码实际上为-1100。 The url should be correct since I checked it in the browser. 该网址应该正确,因为我在浏览器中检查了该网址。 Anyone knows why? 有人知道为什么吗?

let userImageURL: String! = "http://i.imgur.com/QhCzQoR.jpg";
let url = NSURL(fileURLWithPath: userImageURL);
let request:NSURLRequest = NSURLRequest(URL: url!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response:NSURLResponse!, imageData:NSData!, error:NSError!) -> Void in
                let image = UIImage(data: imageData!);
 })

The reason you are getting this problem is because you have used 您收到此问题的原因是因为您使用过

let url = NSURL(fileURLWithPath: userImageURL);

Instead you should use: 相反,您应该使用:

let url = NSURL(string: userImageUrl)

I code in Objective-C, it should be easy to convert it in Swift (small modification in fact, but the explanation is not that much related to code). 我用Objective-C编写代码,应该很容易在Swift中进行转换(实际上是很小的修改,但是解释与代码并没有太大关系)。 If you check the error complete message, you'll get: 如果检查错误完成消息,您将得到:

Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." 错误域= NSURLErrorDomain代码= -1100“在此服务器上找不到请求的URL。” UserInfo=0x7997b7e0 {NSErrorFailingURLStringKey=file:///http:/i.imgur.com/QhCzQoR.jpg, NSErrorFailingURLKey=file:///http:/i.imgur.com/QhCzQoR.jpg, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x799f3080 "The requested URL was not found on this server."} UserInfo = 0x7997b7e0 {NSErrorFailingURLStringKey = file:/// http:/i.imgur.com/QhCzQoR.jpg,NSErrorFailingURLKey = file:/// http:/i.imgur.com/QhCzQoR.jpg,NSLocalizedDescription =请求的URL是在此服务器上找不到。,NSUnderlyingError = 0xx7f3080“在此服务器上找不到请求的URL。”}

Clearly, if you look carefully at the URL, you have file:///http:/i.imgur.com/QhCzQoR.jpg , which is not the URL wanted. 显然,如果您仔细查看URL,则有file:///http:/i.imgur.com/QhCzQoR.jpg ,这不是所需的URL。 If you log url.absoluteString , you'll see it to your URL, it's you that set it like this. 如果您登录url.absoluteString ,则会在URL中看到它,是这样设置的。

Why? 为什么? Because you used fileURLWithPath: instead of URLWithString: . 因为您使用的是fileURLWithPath:而不是URLWithString: fileURLWithPath: So change your line with: 因此,请更改您的行:

let url = NSURL(URLWithString: userImageURL);

Some discussions about the differences between theses two: What is difference between URLWithString and fileURLWithPath of NSURL? 关于这两者之间的区别的一些讨论: NSURL的URLWithString和fileURLWithPath之间有什么区别? or the doc . doc

Update: In Swift 4: 更新:在Swift 4中:

let url = URL(string: userImageURL)

We also recommend from Swift3+ to avoid NSStuff when there is Swift Equivalent: NSURL => URL . 我们还建议Swift3 +在存在Swift等效项时避免使用NSStuff: NSURL => URL

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

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