[NSCFArray length]:发送到实例的无法识别的选择器

时间:2014-12-09 13:54:26

标签: ios objective-c xcode nsarray

我正在尝试用数组中的一些数据填充表视图。数组已填充,但当我运行应用程序时,我收到此错误:原因:' - [__ 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;
}

2 个答案:

答案 0 :(得分:5)

错误表示此行:

[self.currentDate objectAtIndex:indexPath.row]

返回一个Array而不是NSString。这意味着

self.currentDate

包含NSArray对象,而不包含NSString对象。

答案 1 :(得分:1)

阿里斯是对的。 self.currentDate会返回NSArray。这是由:

引起的
[scorearray addObject:highScores];
[dataArray addObject:data];

将一个数组作为唯一项添加到数组scorearraydataArray的位置。你没有添加一串字符串,我认为这些字符串存储在highScoresdata中。 替换为

[scorearray addObjectsFromArray:highScores];
[dataArray addObjectsFromArray:data];

这应该将highScoresdata的内容添加到scorearraydataarray