2010-07-14 66 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.