0

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?

esad
  • 2,660
  • 1
  • 27
  • 23

4 Answers4

1

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

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.

Abizern
  • 146,289
  • 39
  • 203
  • 257
1

There's an entire Apple programming guide dedicated to naming conventions and style in Cocoa.

Rob Keniger
  • 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
0

Since your subclass will tend to have your own prefix (like EHMapView) you may prefix instance variables with _eh_ (e.g. _eh_foo).

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • This actually conflicts with Apple's claim on the underscore prefix. – Chuck Mar 04 '11 at 20:25
  • 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