内容类型JSON SIGABRT错误

时间:2013-02-13 19:36:32

标签: 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];

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

1 个答案:

答案 0 :(得分:1)

您收到错误是因为您正在使用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"];