0

我有一个充满问题和答案(带有指定问题)的列表,显示在界面上,但我想随机显示问题。请参阅我在下面尝试的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (questions && configDictionary) {

        for (int i = 0; i < [questions count]; ++i) { int r = (random() % [questions count]); [questions exchangeObjectAtIndex:i withObjectAtIndex:r]; }

        [questionLabel setText:[[questions objectAtIndex:currentQuestonIndex] objectForKey:@"question"]];
        NSArray *answers = [[questions objectAtIndex:currentQuestonIndex] objectForKey:@"answers"];
        [answerLabel0 setText:[answers objectAtIndex:0]];
        [answerLabel1 setText:[answers objectAtIndex:1]];
        [answerLabel2 setText:[answers objectAtIndex:2]];
        [answerLabel3 setText:[answers objectAtIndex:3]];
        [pointsPerAnswerLabel setText:[NSString stringWithFormat:@"+%d points", [[configDictionary objectForKey:kPointsPerCorrectAnswer] intValue]]];
        [currentQuestionNumberLabel setText:[NSString stringWithFormat:@"question %d", currentQuestonIndex+1]];
    }
}

Someone please help?

我收到警告“NSArray”可能无法响应“exchangeObjectAtIndex:withObjectAtIndex:”然后它在尝试运行后崩溃。

请参阅下面的 .h 文件:

@interface ViewController : UIViewController {
    NSDictionary *configDictionary;
    NSMutableArray *questions;
    int currentQuestonIndex;
    NSMutableArray *questionsCorrectlyAnswered;
    NSTimer *timer;
    int totalTimeLeft;
    int currentTimeLeft;

    BOOL saveTime;
}
@property (retain, nonatomic) NSMutableArray *questions;

它仍然崩溃。

我以前查看的表格视图:

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    gameViewController = [[QuizViewController alloc] initWithNibName:@"QuizViewController"bundle:nil];
    [(QuizViewController*) gameViewController setMasterViewController:self];
    [(QuizViewController*) gameViewController setTitle:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"quizName"]];
    [(QuizViewController*) gameViewController setQuestions:[[quizzes objectAtIndex:indexPath.row] objectForKey:@"questions"]];
    [gameViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:gameViewController animated:YES];
}
4

2 回答 2

1

无论您在哪里设置“ questions”(您不会在代码中显示),您都需要将其设置为“ NSMutableArray”而不是不可变(不可更改、不可交换)“ NSArray”。

于 2012-08-09T18:18:56.947 回答
0

这是我的尝试。不是您问题的确切答案,但我过去确实做过类似的事情。我所做的是我有一系列评论,我想在每次启动应用程序时随机化。所以我就是这样做的。

NSMutableArray *funnyArray =[[NSMutableArray alloc] initWithObjects:
                                 @"Got Milk?",
                                 @"Got Food?",
                                 @"Life Is Like A Box of Chocolates", nil];

//Randmonize Array
    NSUInteger count = [funnyArray count];

    for (NSUInteger i = 0; i < count; ++i) 
    {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [funnyArray exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

现在在您的情况下,您将仅随机化问题。但是您希望问题和答案对随机排列在一起。我不确定你能怎么做。也许困难的方法是在您的数组中的问题旁边放一个数字,例如 1。我的牛奶在哪里?当您从数组问题中获得随机 str 时,您可以去掉“1”并在答案数组中查找“1”。我建议像其他人建议的那样使用 nsmutablearrays。只是我的2美分。

于 2012-08-10T01:43:50.363 回答