1

Possible Duplicate:
Style: Dot notation vs. message notation in Objective-C 2.0

In apple documentation for NSArray it says: "Instance method>" "count". So, normally I would write [[NSArray array] count]. However, [NSArray array].count works just fine... And recently I figured out that UIScreen.mainScreen.bounds is also accepted. mainScreen is a class method for UIScreen.

Anyone thought about which style to use (and why)? Please comment.

Community
  • 1
  • 1
debleek63
  • 1,181
  • 8
  • 17
  • +1 for a good question, but voting to close because it is, in fact, a duplicate. More importantly, the answers in the duplicated Q are significantly better. – Caleb Nov 04 '11 at 19:00
  • @Caleb, You're right, they are. That question should be the first source. – Matt Wilding Nov 04 '11 at 19:05

2 Answers2

3

IMO, dot syntax implies a certain passivity; using it shouldn't have significant side effects. It might be okay to treat NSArray's -count method as a property even though it isn't actually one (it probably would be if properties had been part of the language when NSArray was created), but saying someMutableArray.removeLastObject would offend my sensibilities even though it'd probably work just fine.

One thing you should know about dot notation, though, is that you can't mix Objective-C's dot notation with C's dot notation on the left side of an assignment. In other words, don't try this:

myView.bounds.origin.x = 50.

You must instead say:

CGRect newBounds = myView.bounds;
newBounds.origin.x = 50;
myView.bounds = newBounds;
Caleb
  • 124,013
  • 19
  • 183
  • 272
3

Dot syntax for is just syntactic sugar for calling methods. So [NSArray array].count is identical to [[NSArray array] count], and UIScreen.mainScreen is identical to [UIScreen mainScreen]

You should never use dot syntax for anything other than property access. For property access, the choice is a matter of personal style.

Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
  • 2
    I think it's fine to use dot notation for methods that act like properties even if they're not declared as properties. NSArray's `count` method is one good example; NSDictionary's `allKeys` and `allValues` are two more. – Caleb Nov 04 '11 at 18:35
  • @Caleb, I can understand that opinion. I was speaking from an official apple-supported policy perspective. I personally avoid the dot syntax, so it's not really an issue for me. – Matt Wilding Nov 04 '11 at 18:39
  • 1
    I'm not aware of a clear official statement from Apple on this question. Do you know of one? As explained in [The Objective-C Programming Language](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW17), dot syntax is used for accessors, but I don't read that to mean that you have to limit use to properties declared using @property syntax. – Caleb Nov 04 '11 at 18:56
  • 1
    @Caleb, like you said, the docs state the dot syntax is used for accessors, and it defines accessors as "an instance method that gets or sets the value of a property of an object". I interpret that "property" to mean an Objective-C Property, but I suppose you could also interpret it as a reference to the idea of a property, not a specific language feature. Either way, it's incredibly nit-picky, so you're right that your use case is almost certainly acceptable. – Matt Wilding Nov 04 '11 at 19:15