什么时候NSURLResponse不是NSHTTPURLResponse? [英] When is a NSURLResponse not a NSHTTPURLResponse?

查看:235
本文介绍了什么时候NSURLResponse不是NSHTTPURLResponse?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多代码,包括Apple的SimpleURLConnections示例,它只是将NSURLResponse转换为NSHTTPURLResponse。如果它始终是NSHTTPURLResponse为什么NSURLConnections不返回NSHTTPURLResponse?

I've seen a lot of code, including Apple's SimpleURLConnections sample, that simply cast any NSURLResponse to a NSHTTPURLResponse. If it is always a NSHTTPURLResponse why do the NSURLConnections not return NSHTTPURLResponse?

我担心如果我简单地转发响应,我会引入错误的代码。

I'm worried that if I simply downcast the response, I'm introducing buggy code.

例如,如果不检查isKindOfClass就可以吗?

For instance, is it OK to do this without checking isKindOfClass?

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)redirectResponse;
 // do stuff
}


推荐答案

如果您确定您的连接是通过HTTP协议运行的,那就没关系了:

It is ok if you are sure that your connection runs via HTTP protocol:


NSHTTPURLResponse对象表示对HTTP URL加载的响应请求。它是NSURLResponse的子类,提供访问特定于HTTP协议响应的信息的方法。

An NSHTTPURLResponse object represents a response to an HTTP URL load request. It’s a subclass of NSURLResponse that provides methods for accessing information specific to HTTP protocol responses.

例如,如果您通过FTP连接,然后将 NSURLResponse 转换为 NSHTTPURLResponse 将不正确。

If you are connecting via FTP, for example, then casting NSURLResponse to NSHTTPURLResponse will be incorrect.

这篇关于什么时候NSURLResponse不是NSHTTPURLResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆