Reputation: 61
I have a plist which contains a number of quiz item are pulled randomly by using arc4random (), I want to remove each item after it's shown so that no questions are repeated, I've tried using [thisArray removeObjectAtIndex:r];
, but the items still show. Any ideas?
NSString *path = [[NSBundle mainBundle] pathForResource:@"initialquestions" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSLog(@"The file exists");
}
else {
NSLog(@"The file does not exist");
}
NSMutableArray *thisArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"The array count: %i", [thisArray count]);
for (int i = 0; i < [thisArray count]; i++) {
id obj;
int r = arc4random() % [thisArray count];
if(r<[thisArray count]){
obj=[thisArray objectAtIndex:r];
}
else{
//error message
}
QuestionTitle.text = [[thisArray objectAtIndex:r] objectForKey:@"QuestionTitle"];
[btnA setTitle:[[thisArray objectAtIndex:r] objectForKey:@"A"] forState:UIControlStateNormal];
[btnB setTitle:[[thisArray objectAtIndex:r] objectForKey:@"B"] forState:UIControlStateNormal];
[btnC setTitle:[[thisArray objectAtIndex:r] objectForKey:@"C"] forState:UIControlStateNormal];
[thisArray removeObjectAtIndex:r];
}
Upvotes: 0
Views: 143
Reputation: 46543
As your array is shrinking, you need a reverse for loop :
for (int i = [thisArray count]-1; i > -1 ; i--) {
....
}
Upvotes: 1