【发布时间】:2014-12-09 13:54:25
【问题描述】:
我正在尝试用数组中的一些数据填充表格视图。数组已填充,但是当我运行应用程序时出现此错误:原因:'-[__NSCFArray 长度]:无法识别的选择器已发送到实例
我搜索了类似的问题,但没有人适合我。我不使用 lenght 函数,我的应用程序崩溃了:cell.textLabel.text = [self.currentDate objectAtIndex:indexPath.row];
quizViewController.m
- (void)passDataForward
{
ScoreViewController *secondViewController = [[UIStoryboard storyboardWithName:@"Storyboard" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"score"];
secondViewController.data =self.scoreLabel.text;
secondViewController.delegate = self;
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[highScores insertObject:self.scoreLabel.text atIndex:highScores.count];
NSData *currentDate =[NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd.MM.YYYY HH:mm:ss"];
NSString *dateString =[dateFormatter stringFromDate:currentDate];
[data insertObject:dateString atIndex:data.count];
NSMutableArray *scorearray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"high_scoresarray"]];
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"data_score"]];
[scorearray addObject:highScores];
[dataArray addObject:data];
[[NSUserDefaults standardUserDefaults] setObject:scorearray forKey:@"high_scoresarray"];
[[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:@"data_score"];
secondViewController.test=[NSMutableArray arrayWithArray: scorearray];
secondViewController.currentDate=[NSMutableArray arrayWithArray: dataArray];
[self presentViewController:secondViewController animated:YES completion:^(void)
{
}];
}
ScoreViewController.m
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [self.currentDate objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[self.test objectAtIndex:indexPath.row];
return cell;
}
【问题讨论】:
-
当您在 ScoreViewController 的 viewDidLoad 方法中使用 NSLog(@"%@",self.currentDate} 时,您的领域是什么?它是否也崩溃了?顺便说一句,我不确定是否有你的代码中有错别字,但是这一行 NSData *currentDate =[NSDate date]; , NSData and NSDate?
-
你的数据在吗?你的变量的名字真的很奇怪,就像我希望 self.currentDate 是 NSDate 一样,但它似乎是数组。也许这是问题 - 在 line cell.textLabel.text = [self.currentDate objectAtIndex:indexPath.row];
-
你能确认一下,self.currentDate 确实是一个数组吗? (来自 ScoreViewController 的 cellForRow 方法)
-
self.currentDate 是一个数组,当我 NSLog(@"%@",self.currentDate}
-
好的,请尝试在它崩溃的行之前插入 - if([self.currentDate isKindOfClass:[NSArray class]]) { } 并在此处插入断点(在 {} 之间)。然后尝试一下,如果它停在那里。我仍然认为 self.currentDate 不是数组:)
标签: ios objective-c xcode nsarray