Reputation: 127
I'm trying to fill a table view with some data from an array . The array is populated but when i run the app i get this error : reason: '-[__NSCFArray length]: unrecognized selector sent to instance
I searched for similar questions , but no one was the right for me. I don't use the lenght function and my app is crashing on : 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;
}
Upvotes: 0
Views: 2558
Reputation: 14068
Aris is right.
self.currentDate
returns an NSArray
. And that is caused by:
[scorearray addObject:highScores];
[dataArray addObject:data];
Where you are adding one array as the only item to the array scorearray
and dataArray
respectively. You do not add a bunch of strings, which I assume are stored within highScores
and data
.
Replace it with
[scorearray addObjectsFromArray:highScores];
[dataArray addObjectsFromArray:data];
That should add the contents of highScores
and data
to scorearray
and dataarray
.
Upvotes: 1
Reputation: 1559
The error means that this line:
[self.currentDate objectAtIndex:indexPath.row]
returns an Array instead of an NSString. Which means that
self.currentDate
contains NSArray objects and not NSString objects.
Upvotes: 5