2

I have the exact opposite of Xcode 4 dot notation code sense problem! The code completion for dot notation shows not only properties but also methods for me (with P or M marked on the left of each completion appropriately indicating whether it is a property or a method respectively). Is this the normal behavior on Xcode 4 or am I doing something wrong?

This does not happen always though. It appears to happen for classes defined by me but not for pre-defined classes. But I haven't tested enough to be sure of this.

Maybe it is possible to customize code sense but there are no answers as yet for where code sense options in Xcode 4 are which has also been asked for Xcode 3.2.2 at "Customizing Xcode: fonts, code sense and more" again with no answers yet.

Community
  • 1
  • 1
trss
  • 915
  • 1
  • 19
  • 35
  • It appears that method names are not suggested after the dot only for class objects! As BJ Homer said, "count" method of NSArray is suggested after an object of that type followed by dot. – trss Dec 05 '11 at 15:56

1 Answers1

3

ObjC dot notation can be used for any method that takes no parameters; it is not limited to formally declared properties. This is mostly because when dot notaion was introduced to the language there was a large amount of existing code that had -foo and -setFoo: methods, implicitly defining a property. Thus, they decided to enable dot syntax for any conforming method names, even if they weren't part of an explicit @property.

Now, we can debate about whether that was a good decision or not, but that's how it is. myArray.count is perfectly valid code, even though there's no "count" @property.

BJ Homer
  • 48,806
  • 11
  • 116
  • 129
  • I understand that it is valid to use dot notation for non-properties but from the various debates on dot notation the only useful outcome which seems sensible to me is to use it to differentiate properties from methods. My question is, Xcode seems to help in this manner for pre-defined classes but not for user-defined ones. So.. 1. Could you confirm that this happens for you? 2. Is there a way to configure it to make it work that way? Thank you for your answer. – trss Dec 05 '11 at 15:39
  • In fact, it is hard to find out which getters and setters have side effects and which don't in the API classes and hence I would prefer using the box notation for all of the old classes which don't use explicit properties. Since I'm a beginner I'm not sure how to identify which ones are true properties and which ones are not so easily. Is there a way to be sure of this? Even the documentation doesn't seem to categorize the list of tasks that way. – trss Dec 05 '11 at 16:02
  • 1. Xcode treats my classes and framework-provided classes the same as far as offering non-properties for dot-syntax completion on my machine. 2. No, there is no way to configure the completion. 3. Whether a getter has a side-effect or not has nothing to do with whether it's a property. `-[UIViewController view]` is a declared readonly `@property`, and it has a side effect. There is no distinction between "true properties" and old-style ones. The documentation does indicate `@properties` by a lack of `-` or `+` on the method name, but that just indicates it may have an implicit setter. – BJ Homer Dec 05 '11 at 16:37
  • @trss if you're looking for a way to differentiate between properties that have no side effects and those that do... there is none. Even in the case of a synthesized property, someone could add side effects via `-addObserver:forKeyPath:`. – BJ Homer Dec 05 '11 at 16:38
  • 1. Ya, I edited the question confirming that methods are not suggested for class objects after the dot. 2. So basically it's slightly difficult to maintain my style of using dot notation for properties and box notation for methods using code completion. Hope they make code completion configurable in Xcode. 3. I didn't know there were intentional properties with side effects. Thanks for pointing that out. I just used side effect free as an alternative for intentional properties. Also, I have never seen a task without a - or + prefix in the documentation. Could you give me an example? Thank you. – trss Dec 06 '11 at 06:40
  • Adding to point 3, for e.g., the documentation for NSButton contains a - prefix for title and setTitle: methods though I think they are actually for the title property. – trss Dec 06 '11 at 07:33
  • The NSButton.h header actually declares -title and -setTitle: methods. It's not a formal `@property`, though it is an implicit property. This is precisely why dot syntax is allowed for non `@property` properties (like NSButton's "title" property). – BJ Homer Dec 06 '11 at 17:00
  • Got it. Thanks a lot. I also noticed a few properties in NSRegularExpression and a number of them in NSTextCheckingResult. If there were an option to configure code sense, I would make it suggest dot notation only for those that it recognizes as properties. I wouldn't mind using box notation for old style properties. That way, the convention followed may not be ideal but I find it good enough and the ease of use gained is immense. Anyway, thanks BJ Homer for your replies. – trss Dec 09 '11 at 10:34