ios - 内容类型 JSON SIGABRT 错误

标签 ios objective-c json nsurlrequest

我正在尝试解析 JSON,并收到 SIGABRT 错误:reason: '[<NSURLRequest 0x7e7e970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Content-Type.

我的代码是:

self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://dmsfw01.dev.com:8082/G.FQI/GVOLFQI.svc/Search(v)?Start=1&Count=10"]];

NSString *contentType = @"application/json";
[request setValue:contentType forKey:@"Content-Type"];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

我应该怎么做才能设置内容类型?

最佳答案

您遇到错误是因为您使用的是 Key Value Coding尝试根据请求设置名为 Content-Type 的属性的方法。你也不能修改 NSURLRequest 因为它是不可变的。使用 NSMutableURLRequest相反,然后调用 - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                [NSURL URLWithString:@"http://sw730voldmsfw01.vol1dev.com:8082/GVOL.FQI/GVOLFQI.svc/Search(visa)?Start=1&Count=10"]];

NSString *contentType = @"application/json";
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];

关于ios - 内容类型 JSON SIGABRT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861584/

相关文章:

ios - NSPredicate 比较数字问题

ios - Parse.com 存储 boolean 数组

objective-c - NSSplitView - setVertical :YES or setVertical:NO 后强制屏幕更新

iphone - objective-c ,检查文件是否存在于路径中

java - 将图像从 Objective-c 上传到服务器时,我收到损坏的文件

iphone - 将日期字符串与 NSPredicate 进行比较

ios - NSPredicate 和 Core Data 多表查询

c# - ASP.NET 数据注释 : How to share with a JSON client?

json - 在 Flask 中解析嵌套的 JSON

javascript - 如果对象具有属性,则对对象数组进行排序