我尝试在我的自定义NSURLProtocol中返回NSURLResponse的子类的对象,但似乎返回的响应总是由普通的NSURLResponse交换.
这是我返回自定义TestURLResponse的NSURLProtocol的一部分:
TestURLResponse* response = [[[TestURLResponse alloc] initWithURL: [[self request] URL]
MIMEType: @"text/plain"
expectedContentLength: 4
textEncodingName:@"UTF-8"]
autorelease];
[self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[self.client URLProtocol:self didLoadData: [@"Test" dataUsingEncoding:NSUTF8StringEncoding]];
[self.client URLProtocolDidFinishLoading: self];
Run Code Online (Sandbox Code Playgroud)
当我与这个协议读的东西我希望得到类型TestURLResponse的反应,但在下面的代码我总是得到一个NSURLResponse:
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"test://test"]];
NSURLResponse* response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *className = NSStringFromClass([response class]);
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:className message:className delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
Run Code Online (Sandbox Code Playgroud)
整个测试项目可以在下载
现在我不知道我做错了什么或是否有什么东西坏了.NSURLProtocol的文档说明
协议实现者可能希望创建自定义的可变NSURLResponse类以提供协议特定信息.
但在我看来,这根本不起作用.
我试图连接3个序列作为具有yield的循环的个体结果.没有临时变量,我无法让它工作.有人知道更好的选择吗?notWorking版本在第一个++之后的方法"非法启动简单表达式"的第四行给出了编译器错误.
def working() : Seq[Seq[Elem]] = {
val result = for(index <- 0 until COMPLETE_INPUT_CHANNELS) yield {
getModesOfCompleteInputChannel(index)
}
val result2 = for(index <- 0 until INCOMPLETE_INPUT_CHANNELS) yield {
getModesOfIncompleteInputChannel(index)
}
val result3 = for(index <- 0 until OUTPUT_CHANNELS) yield {
getModesOfOutputChannel(index)
}
return result ++ result2 ++ result3
}
def notWorking() : Seq[Seq[Elem]] = {
for(index <- 0 until COMPLETE_INPUT_CHANNELS) yield {
getModesOfCompleteInputChannel(index)
} ++ for(index <- 0 until INCOMPLETE_INPUT_CHANNELS) yield {
getModesOfIncompleteInputChannel(index)
} ++ for(index <- 0 until …Run Code Online (Sandbox Code Playgroud)