Why is the 4th edition constantly using self. self. self.?


#1

The 3rd edition seems now to have code that is more brief as this 4th edition is using dot syntax with self, self, self etc
Is there a good reason for this?
thanks


#2

I suggest you give an example that shows both versions (from the two editions of the book) in order to clarify your question.


#3

From the introduction to the 4th edition:

Just keeping up with the times, I guess! According to the authors, there are times when dot notation improves code readability and times when it can make things look confusing. IMO, it makes code look cleaner and clearer more often than not.


#4

You might want to understand why using ‘self’ is important first. As long as you’re dealing with a language based on manipulating objects/classes like Objective-C, you will be dealing with constantly using ‘self’.


#5

Thanks guys

I understand what self is and what it’s used for.
My fuss was because at times it is better to use the brackets because it means less typing for certain statements. The dot syntax is like an address while brackets get straight to the point with what you are trying to get at.

Guess I will have to just get used to it, oh well :slight_smile:


#6

I still think an example might be useful because I’m surprised there are cases where the dot notation results in more code.


#7

I’d like an example too please, if possible?

IIRC, I have seen both self.items.count and [self.items count] in the book. To me, it’s a combination of both personal preference plus always needing [] to send objects messages.

New additions to Objective-C that I’m very happy to use include myDictionary[item], instead of [myDictionary objectAtIndex:item] . The new @[x,y] and @{x:YES y:NO} constructors for on the fly arrays and dictionaries are awesome too.


#8

This was the part of the book I was on when I asked the question.
There is more syntax to type out using the dot syntax as can be seen in this Action method which does the same work on both occasions.

[color=#0000FF]This is the code from the 3rd Edition:
[/color]

[code]
-(IBAction)showQuestion:(id)sender
{
currentQuestionIndex++;

if (currentQuestionIndex == [questions count])
{
    currentQuestionIndex = 0;
}
   
NSString *question = [questions objectAtIndex:currentQuestionIndex];

[questionField setText: question];

[answerField setText:@"???"];

}[/code]

[color=#0000FF]…And the new code in the 4th Edition:
[/color]

- (IBAction)showQuestion:(id)sender
{
    self.currentQuestionIndex++;

       if (self.currentQuestionIndex == [self.questions count]) 
    {
         self.currentQuestionIndex = 0;
    }

    NSString *question = self.questions[self.currentQuestionIndex];

    self.questionLabel.text = question;

    self.answerLabel.text = @"???";
}

Whats your opinion guys…?


#9

Originally, it seemed that you were suggesting the following:

self.someValue = 42;

was more verbose than:

[self setSomeValue: 42];

So this question is really about why BNR now uses properties instead of ivars in the Quiz application.

There’s an article on the BNR site that covers this http://blog.bignerdranch.com/4005-should-i-use-a-property-or-an-instance-variable/. Have a read and then add to this thread if you feel there is a need.


#10

Its obvious to me that some lines in the 4th edition are longer than the same lines in the 3rd edition… That is what I was questioning.


#11

It’s because BNR switched to using properties instead of ivars between the two editions. There’s nothing to stop you from using ivars and keeping the code the same as it was in the 3rd edition, but properties are preferred now due to some of the reasons listed in the article I referenced.


#12

Thank you sir