5

While programming for iOS I encountered the following scenario:

I have a singleton class with a class method + (Store*)sharedStore. When I want to call an instance method on the singleton object, I can use dot syntax to get that object, i.e. [Store.sharedStore foo].

However, Xcode does not autocomplete 'sharedStore' after typing the dot. On the other hand, [[Store sharedStore] foo] is autocompleted!

Is there such a thing as 'class properties'? If I could turn sharedStore into a readonly property on the class, the dot syntax would gain autocompletion.

More generally speaking, Xcode simply does not autocomplete after dot syntax on anything that isn't a property, even though this is a valid way of calling a (getter) method.

Any solution, workaround, or information is appreciated.

Andrey
  • 2,503
  • 3
  • 30
  • 39
Timo
  • 7,992
  • 4
  • 49
  • 67
  • 1
    It's allowed, so it's probably an Xcode bug. See http://stackoverflow.com/questions/2375943/objective-c-2-0-dot-notation-class-methods – trojanfoe Jan 07 '13 at 10:12
  • 1
    It works if you do instead: [[Store class].sharedStore foo]; – Lluís Jan 07 '13 at 10:23

2 Answers2

3

Currently, as far as class getters go, it appears that we must either:

  • use this syntax in the absence of autocompletion: Store.sharedStore; or
  • use bracket syntax instead: [Store sharedStore].

I have not tried other editors recently (e.g. AppCode). Another editor might autocorrect the dot syntax on class getters.

Opinions vary as to what is correct, logical or readable.

Timo
  • 7,992
  • 4
  • 49
  • 67
0

Dot syntax is for accessing properties. Some people use it with instance methods that return a value and get no arguments, but it's a bad approach. All other scenarios are wrong.

Lluís
  • 578
  • 1
  • 5
  • 10
  • Can you please explain why or according to whom it is a bad approach? After all, such a method has the role of 'getter'. A property creates *exactly* such a method. And what more is an Obj-C property than a shorthand for creating a getter [and setter] method? – Timo Jan 07 '13 at 12:07
  • 1
    Because of the same reason the names of variables or instance variables always begin with a lowercase or a '_', the same reason the name of all classes begin with an uppercase and sometimes with prefix, the same reason macros are written with uppercase and separated with '_', the same reason globals sometimes begin with 'g' and constants with 'k'. Convenience. Dot notation allows you to know at a glance when a method is a getter or a setter of a property. That's how Apple uses it and teaches to use. You can use always dot notation, but this approach will be always worse. Regards. – Lluís Jan 07 '13 at 17:01
  • According to the [documentation](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html) you are correct. According to Xcode, your explanation is too limiting. Preferably, Apple should either fix the documentation or fix this in Xcode. Personally, I hope they fix the documentation: I believe `UIApplication.sharedApplication`, as per my comment [here](http://stackoverflow.com/a/2375991/543814), is correct and makes sense to read. Granted, calling a factory method like `NSArray.array` is illogical. – Timo Jan 14 '13 at 11:20