问题描述
我见过很多代码,包括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:
例如,如果您通过FTP连接,然后将 NSURLResponse
转换为 NSHTTPURLResponse
将不正确。
If you are connecting via FTP, for example, then casting NSURLResponse
to NSHTTPURLResponse
will be incorrect.
这篇关于什么时候NSURLResponse不是NSHTTPURLResponse?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!