iOS:NSJSONSerialization导致"数据参数为零"

Mat*_*han 4 ios nsjsonserialization

我有一个NSURLConnection和所有适当的方法在一个视图控制器中工作.然后我将它移动到UICollectionViewController并在下面获得一个例外

    - (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);
    }
}
Run Code Online (Sandbox Code Playgroud)

错误是: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'data parameter is nil'

有人有主意吗?

ale*_*oot 11

异常消息告诉你变量:self.jsonReceivedData是nil,而你调用的方法 JSONObjectWithData不支持nil数据......

初始化self.jsonReceivedData字段以解决问题;-).