Hello everyone. I'm noticing a very strange problem when trying to access string values from an NSDictionary. I load the data from a URL and parse the JSON with this code, storing only the 'sets' value as an NSArray of NSDictionaries:
Code:
// Prepare URL request to download data
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *responseData = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *results = [responseData JSONValue];
resultArray = (NSArray *)[results objectForKey:@"sets"];
I then try to populate a UITableView with that data using the following code (downloadManager was the class where the code above is located):
Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[downloadManager resultArray] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set up cell
static NSString *cellIdentifier = @"TableCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If a cell doesn't exist, create it.
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Set up cell
if ([[downloadManager resultArray] count] > 0) {
NSDictionary *rowData = [[downloadManager resultArray] objectAtIndex:[indexPath row]]; // <- always produces a crash, without any log info
[[cell textLabel] setText:[rowData objectForKey:@"title"]];
}else{
NSLog(@"No data returned.");
}
// Return the cell
return cell;
}
Maybe I'm missing something obvious, but I simply cannot access the data from the dictionary without the simulator crashing. (Even with NSLog.) Is there something wrong with my code that could be causing this?