我试图获取包含 2 个 twitter 用户提要的 2 个单独的 JSON 数据对象,但遇到了很多麻烦。我想同时处理它们并将它们放在一张桌子上,但我遇到了困难。
我有一个全局 NSMutableData 获取附加到它的每个 JSON 请求。一旦所有这些都在一个地方,我会尝试使用全局 NSMutableData 更新表。allTheData 是全局 NSMutable 对象。
if ([twitterAccounts count] > 0) {
while (accountsize != [twitterAccounts count]) {
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
[allTheData appendData:responseData];
}];
accountsize = accountsize + 1;
}}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self performSelectorOnMainThread:@selector(receiveData:) withObject:allTheData waitUntilDone:YES];
});
- (void)receiveData:(NSData *)data {
self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.TwitterTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
NSDictionary *tweet = [self.tweets objectAtIndex:indexPath.row];
cell.textLabel.text = [tweet objectForKey:@"text"];
return cell;
}
每当我运行此代码时,它都会崩溃并吐出以下内容
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
* First throw call stack: (0x2622012 0x131ae7e 0x2621deb 0xe4f817 0x568c 0x132e6b0 0xd5b035 0x25a5f3f 0x25a596f 0x25c8734 0x25c7f44 0x25c7e1b 0x290d7e3 0x290d668 0x26265c 0x258d 0x24b5) libc++abi.dylib: terminate called throwing an exception