数组有问题

时间:2010-07-14 13:43:07

标签: cocoa-touch debugging arrays

它被分成几行,因为每个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);

}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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.