NSJSONSerialization未被调用

编程入门 行业动态 更新时间:2024-10-24 12:20:39
NSJSONSerialization未被调用 - 目标C.(NSJSONSerialization Not Getting Called - Objective C)

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

我使用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; }

I am trying to populate the TableViewCells in my TableViewContoller with an Array of data that is being pulled from a JSON object. NSJSONSerialization is simply not getting called.

I've tried a variety of things using DataTaskWithURL. Sadly I'm coming up empty with 0 items found in the array. Thanks in advance for anyone's help!

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

You forgot to resume the task

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

更多推荐

本文发布于:2023-08-02 18:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未被   NSJSONSerialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!