iphone - NSURLConnection、NSMutableURLRequest 设置范围错误

标签 iphone

我正在尝试使用以下方法实现下载恢复功能 setValue:forHTTPHeaderField。 但每当我使用该方法时,我都会得到一个

[NSURLRequest setValue:forHTTPHeaderField:]: unrecognized selector sent to instance 0x4e0b710 2011-08-08 22:44:36.469 Patch[9140:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLRequest setValue:forHTTPHeaderField:]: unrecognized selector sent to instance 0x4e0b710'

错误。

如果没有该方法,我的代码可以正常工作,但是当我包含此代码时,我收到上面的错误

NSMutableURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];

//---------------- setting range for download resume -----------------------
NSString* range = @"bytes=";
range = [range stringByAppendingString:[[NSNumber numberWithInt:offset] stringValue]];
range = [range stringByAppendingString:@"-"];
NSLog(@"range: %@", range);

[request setValue:range forHTTPHeaderField:@"Range"];

请帮助我...非常感谢

最佳答案

您需要使用 NSMutableURLRequest 创建一个 NSMutableURLRequest

NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];

关于iphone - NSURLConnection、NSMutableURLRequest 设置范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6983112/

相关文章:

iphone - 添加观察者时是否需要接收NSNotification?

iphone - 在 UINavigationController View 和模态视图中使用相同的 xib 是否可取?

iphone - 在 iOS 上将图像分享到 Instagram

iphone - 在 View Controller 中显示图像和消息并在 IOS 中的特定时间后关闭

objective-c - NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)

iphone - OpenGL 从另一个 View 返回后绘制黑屏

ios - iOS 64 位模拟器上的 va_list 类型

iphone - 范围栏按钮可以用于过滤结果而不激活 UISearchBar 的键盘吗?

ios - App rejected NSUrlConnection IPv6 网络问题 iOS

iphone - UITextView 文本颜色自动变得与背景颜色相同