views:

17

answers:

2

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 = [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);

}

A: 

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
Vladimir
I wasen't aware that it overwrote it. That explains alot. Thanksjust checked it works perfectly!
@shorty876: The code keeps to assign a new array to the same variable. it's equivalent to write `number = 1; number = 2; number = 4; number = 8;`; after executing that code, the variable holds the last value assigned to it.
kiamlaluno
A: 

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.
Nikolai Ruhe