Properties in Objective C

I read through Aaron Hillegass's excellent Cocoa Programming for Mac OS X book and I'm now going through it again, doing the exercises.

The second time round, I found it funny that in the chapter on key-value coding, Aaron mentions how he finds the new dot operator access of properties in Objective C 2.0 to be "a rather silly addition to the language since we already have a syntax for sending messages". Although I agree that having one way of doing things — also known as consistency — causes less confusion, it's unfortunate that this statement comes right after the section in which you write a method to increment an instance variable via its accessor methods:

-(IBAction)incrementFido:(id)sender
{
  [self setFido:[self fido]+1];
}

(In case the syntax of Objective-C throws you off, setFido and fido are accessor methods — in ActionScript the equivalent would be setFido(fido()+1) given that we don't have to explicitly refer to this (self in Objective-C) within an instance method unless there is ambiguity between a parameter name and a property name.

The irony is that, using the dot operator, the above method becomes:

-(IBAction)incrementFido:(id)sender
{
  self.fido++;
}

Thereby demonstrating a good use case for dot notation. :)

Comments