I am trying to load JSON from a sever and then display it in a UITableView. The request goes fine however when I try to add the data to the tableview the app crashes when I call then [tableView reloadData] method is called. I have the variable jsonArray reference in the UITableView methods so that the TableView displays the contents of that variable. Is this the best way to do it and what am I doing wrong?
The connection
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Main HTTP Callback
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
//Declare the parser
SBJsonParser *parser = [[SBJsonParser alloc] init];
jsonArray = [parser objectWithString:responseString];
[tableView reloadData];
[parser release];
[responseString release];
}
Error
2011-07-07 14:42:56.923 Viiad[16370:207] -[__NSSet0 objectAtIndex:]: unrecognized selector sent to instance 0x5a4a0b0
2011-07-07 14:42:56.925 Viiad[16370:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 objectAtIndex:]: unrecognized selector sent to instance 0x5a4a0b0'
EDIT
UITableViewMethods
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSArray *results = [[jsonArray valueForKey:@"Rows"] valueForKey:@"name"];
cell.textLabel.text = (NSString *)[results objectAtIndex:indexPath.row];
return cell;
}
This message
means that someplace in the code, the
objectAtIndex:message was sent to an instance of__NSSet0. To me, that looks like you’re passing around anNSSetwhere you’re expecting anNSArray.Set a debugger breakpoint before you call
[results objectAtIndex:indexPath.row]and see whatresultsactually contains. My guess is that the JSON parser isn’t returning what you think it is. Objective-C is dynamically typed: just because you say that a variable is anNSArraytype doesn’t mean it can’t contain some other object. If a function returns typeid, there is no type information for the compiler to check at all.