Lets say a class has a plain old no-args method declared like this:
- (NSString *)responseString;
There are two ways to call this method. Either using
string = [instance responseString];
or, with the dot syntax, like this:
string = instance.responseString;
What is the preferred way? Any reasons to avoid the last approach?
Update:
responseString is a property of the class, except it is not declared with @property. It is, by definition, an accessor method (getter). The generated code is exactly the same.
(We assume objc v2.0+ here.)
My preferred way is always to use the former because I think dot notation is an abomination that should never have been added to the language.
Putting aside my (justified) prejudice for a moment, you should only use dot notation for things that are conceptually properties (i.e. they don’t necessarily have to use
@propertyto declare them but they should be attributes of the object as opposed to operations the object performs). The name of the method in this case is a noun, so the chances are that-responseStringis a property and it’s OK to use dot notation.An example of something it would not be appropriate to use dot notation for is
NSMutableArray‘s-removeAllObjectsmethod.NB: regardless of whether you use dot notation or not, it is a good idea to always use
@propertyto declare things that are conceptually properties.