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 に答える