开发者

Problem with an array

Its broken into lines because each arrayWithObjects: adds a new question (its a quiz game).

But how come when its NSLogged it only displays the last arrayWithObjects: line. Is it overwriting the others or something?

-(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 = [NSMutableArr开发者_JAVA技巧ay 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);

}


Not sure what behavior you expect but yes, each line overwrites array value assigned in previous line. If you want to accumulate values in your array you can add them using -addObjectsFromArray: method:

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


You probably want an array of dictionaries:

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜