IOS: NSJSONSerialization results in "data parameter is nil"

I had an NSURLConnection and all relevant methods working in the same view controller. Then I moved it to UICollectionViewController and got an exception below

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSError *jsonParsingError = nil; //error right here! NSString *object = [NSJSONSerialization JSONObjectWithData:self.jsonReceivedData options:NSJSONReadingMutableContainers error:&jsonParsingError]; if (jsonParsingError) { NSLog(@"JSON ERROR: %@", [jsonParsingError localizedDescription]); } else { NSLog(@"LIST: %@", object); } } 

Error: * Application termination due to the uncaught exception "NSInvalidArgumentException", reason: "data parameter is nil"

Does anyone have any ideas?

+4
source share
1 answer

The exception message tells you that the variable: self.jsonReceivedData is nil, and the method you call JSONObjectWithData does not support nil data ...

Initialize the self.jsonReceivedData field to solve the problem ;-).

+11
source

All Articles