2013-02-13 46 views
2

我試圖解析JSON,並得到一個SIGABRT錯誤:reason: '[<NSURLRequest 0x7e7e970> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Content-Type.的Content-Type JSON SIGABRT錯誤

我的代碼是:

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

由於您使用的是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"]; 
+1

...還有,使用'setValue:forHTTPHeaderField'方法,而不是'setValue:forKey' – lxt 2013-02-13 19:40:22