Pointer vs. Non-pointer question (showAnswer)


#1

I went through the tutorial once while reading the book and then tried reprogramming the app on my own without assistance (did most of it, but had to look up a few pieces). My question was in the book the showAnswer method is as below.

[code]

  • (IBAction)showAnswer:(id)sender
    {
    //Find out answer to the current question using currentQuestionIndex
    NSString *answer = self.answers[self.currentQuestionIndex];

    //Show the answer in the answer label
    self.answerLabel.text = answer;

}[/code]

When I did the solution myself I used the following:

-(IBAction)showAnswer:(id)sender { //Will this work? //change the text of the answerLabel to be the same index as the answers array self.answerLabel.text = self.answers[self.currentQuestionIndex]; }

What is the advantage of using the pointer (as in the book) versus the way that I wrote it when I redid the exercise? Both get the the correct answer but I know that one of the big advantages in C is pointers but don’t know why it would be.

One more question, does anyone have a list of simple shortcuts for a noob to help with their typing?


#2

The only real reason to do it in two steps is that it becomes easier to read what actually is happening.
My guess is the authors in the book did it this way so that the people reading it (US!) being mostly greenhorns on the subject would be able to grasp what is happening a bit easier.
If they would’ve nested everything together (that’s what you did is called, nesting) I’m pretty sure it would be a bit harder to comprehend the code.

Another reason could be that, while this code is really fairly short, these kind of message sends can become quite a bit more complicated with a lot more arguments as opposed to the one used here. If you would nest the entire thing together spanning over more than one line, with a bunch of brackets all over the place it’s really quite impossible to read it back and understanding what’s going on. In those kind of scenario’s it’s really a good practice to set your variables to be used first and then use them in the message send. A lot cleaner!

As to your other question: What kind of shortcuts are you thinking of?


#3

Thank you for the clarification. I know some java and python so I was just thinking of nesting my code. However I could understand that in a large amount of code this sort of nesting could become confusing (and a source of bugs!)

The one thing I’m thinking of is sometimes if I forget to use code completion I would want to tab to the end of a line so I can put in my curly brackets (or I’ve encountered it twice where code completion didn’t put in my curly brackets for a method).


#4

Did you take a look at the tear-out page at the end of your book?
I use the alt + arrow combo A LOT going through my code.

Or am I misunderstanding your meaning?


#5

I didn’t see the tear out because I have a digital version. Thank you, this shortcut is perfect for what I need.


#6

It’s not a big deal for this example, but it’s useful in other scenarios to store a value in a temporary variable so that if some other object decides to change it behind your back, you still have a copy of what you expect.