2

我正在尝试解析 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];

我应该怎么做才能设置 Content-Type?

4

1 回答 1

1

您收到错误消息,因为您使用的是键值编码方法,该方法尝试根据请求设置名称为 的属性Content-Type。您也不能修改 anNSURLRequest因为它是不可变的。使用 anNSMutableURLRequest代替,然后调用- (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"];
于 2013-02-13T19:39:22.040 回答