0

インターフェイスに表示される質問と回答 (指定された質問を含む) でいっぱいの plist がありますが、質問をランダムに表示したいと考えています。以下で試したコードを参照してください。

- (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" " として設定する必要があります。NSMutableArrayNSArray

于 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. 私の牛乳はどこにありますか? 配列の質問からそのランダムな文字列を取得すると、「1」を取り除き、回答配列で「1」を探すことができます。他の人が提案したように、nsmutablearrays を使用することをお勧めします。ちょうど私の2セント。

于 2012-08-10T01:43:50.363 に答える