When subclassing a class like MKMapView, is there a preferred way of naming the newly added instance variables? Apple says it reserves the underscore prefix for their own use, so can I just go ahead and use whatever I like without worrying about possible clashes?
-
I heard they prefer "Bob" over "Robert." ;-) – Joshua Nozzi Mar 04 '11 at 20:46
4 Answers
You'll want to use a name not used by any of your superclasses — the compiler will error out if you accidentally do and you'll just have to change the variable's name. In general, it's not a very big deal and you can use pretty much whatever you want. It's my observation that category methods are more prone to naming conflict problems than instance variables are.

- 234,037
- 30
- 302
- 389
To be clearer; Apple reserve the underscore prefix for method names not iVars.
Many developers prefer to name their iVars with an underscore prefix to distinguish them from their property names.

- 146,289
- 39
- 203
- 257
There's an entire Apple programming guide dedicated to naming conventions and style in Cocoa.

- 45,830
- 6
- 101
- 134
-
I've read it through, but section on ivars is very short and doesn't address this very specific problem – esad Mar 06 '11 at 00:23
Since your subclass will tend to have your own prefix (like EHMapView) you may prefix instance variables with _eh_
(e.g. _eh_foo
).

- 48,927
- 17
- 132
- 168
-
-
I still use the _ prefix for an easy distinction between private variables and public properties. From what I've read on the matter the bigger problem is prefixing private (category) methods. – Marc Charbonneau Mar 04 '11 at 20:55