0

我有一个包含许多测验项目的 plist 是通过使用 arc4random () 随机拉出的,我想在显示后删除每个项目,以免重复问题,我尝试使用[thisArray removeObjectAtIndex:r];,但项目仍然显示。有任何想法吗?

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];

}
4

1 回答 1

1

随着您的数组缩小,您需要一个反向 for 循环:

for (int i = [thisArray count]-1; i > -1 ; i--) {
    ....
}
于 2013-01-29T09:39:09.740 回答