views:

140

answers:

4

Let's say I have this interface:

// .h
@interface DataObject : NSObject {
    NSString* value;
}

@property (retain) NSString* value;

@end

// .m

@implementation DataObject

@synthetize value

@end

As far as I understand, the following two snippets are identical:

DataObject *o = [[[DataObject alloc] init] autorelease];
[o setValue: @"Hello"];

DataObject *o = [[[DataObject alloc] init] autorelease];
o.value = @"Hello";

Am I correct? If yes, should I prefer one over another one? Or is it just a style preference?

Thank you.

+1  A: 

I think the dot notation is preferable over calling the set method. I think the code is more readable, and the dot notation more clearly identifies that you're setting a property, as opposed to just calling a method on the class.

Andy White
+4  A: 

It is a matter of style.

Dot syntax works in many places throughout Objective C, although it is only considered "acceptable" for getters and setters.

mmc
+1  A: 

Dot notation is just syntactic sugar, so the two are identical.

Abizern
A: 

In August 2009, there was a brouhaha in the blogosphere regarding use of dot syntax.

Kristopher Johnson