views:

113

answers:

2

I am trying to get JSON data loaded from a NSURLConnection delegate to send the array of objects back to the tableview that called it.

The delegate object is initialized with callback to send back to

    NSArray *returnArray;
ResultsTableRoot *callback;

JSON handling method

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {      
[connection release];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
returnArray = [json objectWithString:responseString error:&error];
[responseString release];

//////////////////////////////////////////////
// Send data back to table view
[callback resultsArrayReciever:returnArray];

}  

The array can't be accessed from here, the tableview I want to have the information, however the method is called

-(void)resultsArrayReciever:(NSArray *)array{
        // Code executed    
if(array){
             // Code never executes, array isnt there

}

If you have a better way to go about this whole thing, it is more than welcome!!

A: 
  1. Check the NSError instance to see if there wasn't some problem while deserializing the JSON;
  2. Try retaining the object:

    NSError *error;
    SBJSON *json = [[SBJSON new] autorelease];
    returnArray = [[json objectWithString:responseString error:&error] retain];
    [responseString release];
    [callback resultsArrayReciever:returnArray];
    [returnArray release];
    
Adrian Kosmaczewski
A: 

The returnArray is probably autoreleased. Try retain/releasing it in your methods.

If it is autoreleased the contents will be released in your run-loop and therefore disappear by the time you want to access it.

Niels Castle