0

J'essaie de remplir les TableViewCells dans TableViewContoller avec un tableau de données extrait d'un objet JSON. NSJSONSerialization n'est tout simplement pas appelé. J'ai essayé une variété de choses en utilisant DataTaskWithURL. Malheureusement, je viens vide avec 0 éléments trouvés dans le tableau. Merci d'avance pour l'aide de qui que ce soit!NSJSONSerialization Not Gettinged - Objectif C

@property NSArray *toothpastes; 

@end 

@implementation ToothpastChoicesTableViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSString *urlString = @"https://s3.amazonaws.com/mmios8week/toothpastes.json"; 
    NSURLSession *session = [NSURLSession sharedSession]; 

    [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^ 
    (NSData *data, 
     NSURLResponse *response, 
     NSError *error) { 

     if (error) { 
     NSLog(@"%@", error); 
    } else { 

    self.toothpastes = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 
    [self.tableView reloadData]; } 

    }]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSLog(@"%lu", (unsigned long)self.toothpastes.count); 

    return self.toothpastes.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath]; 

    cell.textLabel.text = self.toothpastes[indexPath.row]; 

    return cell; 
} 
+0

imprimer l'erreur. – vadian

+0

hi @vadian - J'ai essayé de consigner l'erreur mais rien ne s'affiche. J'ai mis à jour le code pour l'inclure. Pensées? Merci d'avoir jeté un coup d'oeil. –

+0

Le bloc d'achèvement est-il appelé? Connectez 'self.toothpastes' - et vous devriez recharger la vue de table sur le fil principal. – vadian

Répondre

0

Vous avez oublié de reprendre la tâche

NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

    if (error) { 
     NSLog(@"%@", error); 
    } else { 

    self.toothpastes = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; 
    [self.tableView reloadData]; 
    } 
    }]; 
[task resume]; 
+0

merci @vadian! Fonctionne comme un charme. Appréciez l'aide monsieur. –