J'essayais d'analyser un JSON string from USGS. Cependant, je reçois erreur "-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x68a34c0"
Fondamentalement, je voudrais analyser le USSON JSON dans la tableview. Quelqu'un peut-il aider? Voici mes codes.iOS 5 JSON à l'erreur tableView
ViewController.h
@interface EarthquakeViewController : UITableViewController
{
NSArray *json;
NSDictionary *earthquakeReport;
}
ViewController.m
- (void)viewDidLoad
{
//content = [NSMutableArray arrayWithObjects:@"one", @"two", nil];
[super viewDidLoad];
[self fetchReports];
}
- (void)fetchReports
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: @"http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour"]];
NSError* error;
json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", json);
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return json.count;
}
- (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];
}
earthquakeReport = [json objectAtIndex:indexPath.row];
NSString *country = [[[earthquakeReport objectForKey:@"features"] objectForKey:@"properties"] objectForKey:@"place"];
NSString *mag = [[[earthquakeReport objectForKey:@"features"] objectForKey:@"properties"] objectForKey:@"mag"];
cell.textLabel.text = country;
cell.detailTextLabel.text = mag;
return cell;
}
L'erreur est montrant à la ligne earthquakeReport = [json objectAtIndex:indexPath.row];
On dirait que vous avez déclaré 'json' en tant que' NSArray' et en essayant d'accéder à certains objets dans le tableau. Mais 'JSONObjectWithData: options: error:' retournera un objet 'id' et il semblerait que JSON soit un ditctionary où vous ne pouvez pas utiliser' objectAtIndex: '. – Pfitz
Ne pas éditer les titres des questions pour dire "résolu". La réponse acceptée est de savoir comment nous savons qu'il est résolu, l'édition est juste du bruit. – jrturton