NSJSONSerialization 未被调用 - Objective C

NSJSONSerialization Not Getting Called - Objective C

我正在尝试使用从 JSON 对象中提取的数据数组填充我的 TableViewContoller 中的 TableViewCells。 NSJSON序列化根本就没有被调用。

我已经使用 DataTaskWithURL 尝试了多种方法。可悲的是,我在数组中找到了 0 个项目,结果是空的。提前感谢任何人的帮助!

@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;
}

您忘记恢复任务

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];