Missing First Question?


#1

Hi Joe / Arron, I have a question regarding the application in Chapter_1. When you first run the application currentQuestionIndex is zero-ed as you mention on page_15, however when you first call “showQuestion” you immediately increment it to 1 with the result that on first execution you miss the first question and answer. Should the increment not be at the very end of “showAnswer” ready for the next question?

BYW: love the book so far, a nice change of style and content, much appreciated.

gary | fuzzygoat


#2

Yes, it will start with the 2nd question in the array, then on to the third, then back to the first, etc. Because it is cycling through these questions and the order doesn’t matter (a user wouldn’t say, “Hey! That’s not the first question!”), we chose to just keep it very simple and focus on the iPhone-specific material.

Normally, you would probably write the following instead:

currentQuestionIndex = (currentQuestionIndex + 1) % [questions count];

But then, we would have to explain how exactly this works, which does take a second to take in, leaving the reader thinking about something other than what we are really trying to explain. In writing the book, we had to try and support a wide range of programmer experience while effectively getting iPhone concepts to people. As such, we simplify in places we can; experienced programmers will know how to interpret and adjust overly simplified code while inexperienced programmers won’t get confused. You will probably see similar situations later in the book.


#3

Thanks Joe, makes perfect sense, just wanted to check your intentions.

much appreciated …
Gary


#4

Or you can just add:

currentQuestionIndex = -1;

to the init method … That’s what I did.