从PLIST中删除NSMutableArray中的项目

时间:2013-01-29 09:18:03

标签: iphone xcode nsmutablearray plist

我有一个包含许多测验项目的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];

}

1 个答案:

答案 0 :(得分:1)

当你的阵列缩小时,你需要一个反向循环:

for (int i = [thisArray count]-1; i > -1 ; i--) {
    ....
}