2010-07-14 97 views
0

因爲每個arrayWithObjects:都添加了一個新問題(它是一個問答遊戲),所以它被分解成幾行。數組問題

但是,如何當它的NSLogged它只顯示最後arrayWithObjects:線。它是否覆蓋別人或什麼?

-(void)loadQuizFromArrays{ //ALL QUESTION DATA GOES HERE 
    /*theQuiz = [NSArray arrayWithObjects:@"question",@"possibleAnswer1",@"possibleAnswer2",@"possibleAnswer3",@"possibleAnswer4",@"correctAnswersNumber",nil];*/ 

    theQuiz = [NSMutableArray arrayWithObjects:@"Which is NOT a StarWars movie?",@"Attack of the Jedi",@"A New Hope",@"The Phantom Menace",@"Attack of the Clones",@"1", nil]; 
    theQuiz = [NSMutableArray arrayWithObjects:@"In what state is the majority of Yellow Stone National Park in?",@"Ohio",@"California",@"Wyoming",@"Nebraska",@"3",nil]; 
    theQuiz = [NSMutableArray arrayWithObjects:@"In the US, what household pet is the most common?",@"Cats",@"Dogs",@"Hamsters",@"Komodo Dragons",@"2",nil]; 
    theQuiz = [NSMutableArray arrayWithObjects:@"A plane is traveling 2675 miles every 5 hours. How many miles does the plane travel in 1 hour?",@"535",@"325",@"540",@"420",@"1",nil]; 

NSLog(@"%@",theQuiz); 

}

回答

0

不知道你所期望的行爲,但是,是的,每條線將覆蓋前行指定數組值。如果你希望積累你的數組中的值,你可以使用-addObjectsFromArray:方法添加它們:

theQuiz = [NSMutableArray arrayWithObjects:@"Which is NOT a StarWars movie?",@"Attack of the Jedi",@"A New Hope",@"The Phantom Menace",@"Attack of the Clones",@"1", nil]; 
[theQuiz addObjectsFromArray: [NSMutableArray arrayWithObjects:@"In what state is the majority of Yellow Stone National Park in?",@"Ohio",@"California",@"Wyoming",@"Nebraska",@"3",nil]]; 
...etc 
+0

我wasen't知道它可以覆蓋它。這解釋了很多。謝謝 只是檢查它完美的作品! – user377419 2010-07-14 13:55:13

+0

@ shorty876:代碼一直給同一個變量分配一個新的數組。這相當於寫入'number = 1;數字= 2;數字= 4;數字= 8;';在執行該代碼之後,該變量保存分配給它的最後一個值。 – kiamlaluno 2010-07-14 14:10:33

0

你可能想詞典的數組:

NSMutableArray* theQuiz = [NSMutableArray array]; 

[theQuiz addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
    @"Which is NOT a StarWars movie?", @"question", 
    @"Attack of the Jedi",    @"choice1", 
    @"A New Hope",      @"choice2", 
    @"The Phantom Menace",    @"choice3", 
    @"Attack of the Clones",   @"choice4", 
    [NSNumber numberWithInt:1],   @"correctChoice", 
    nil] 
]; 
// ... add more questions like above.