Re: Objective-C Instance Variable Names
Re: Objective-C Instance Variable Names
- Subject: Re: Objective-C Instance Variable Names
- From: Ricky Sharp <email@hidden>
- Date: Mon, 7 Apr 2008 15:23:49 -0500
On Apr 7, 2008, at 10:26 AM, Jim Correia wrote:
There is already the possibility of KVC collisions regardless of how
you name your iVars.
Consider...
NSBaseClass, and MyClass derived from it.
Targetting 10.4, MyClass has an iVar named mImportantProperty (using
m prefix here only for argument's sake), and accessors -
setImportantProperty:, -importantProperty.
10.5 comes along and NSBaseClass gets a private iVar named
_importantProperty, and the framework code uses KVC to access this
property (perhaps through the private accessors -
_setImportantProperty:, -_importantProperty.)
My code has violated the don't use the _ prefix for your method
names rule.
My code hasn't used the don't use the _ prefix on my iVar names rule/
suggestion/convention.
Yet, I still have the KVC conflict. (And yes, it will probably be a
pain in the neck to track down.)
I got burned by this a few years back. It was extremely difficult to
track down and I even filed a DTS incident for help with it. The main
reason it was difficult was that it was in the implementation of a
custom control's method:
- (int)currentValue
{
return [[self cell] currentValue];
}
Compiler didn't generate any issues. currentValue did exist on my
corresponding cell's API, but when Tiger was upon us, currentValue was
also a method of NSAnimation I believe. Add onto this the fact that
[self cell] returns an id, and at runtime, currentValue was sent, but
to what the runtime thought was an NSAnimation instance. This really
trashed memory, but only with my control's instance. Very strange
runtime behavior followed.
Since then, I do the following to never ever collide. Some may call
it overkill, but it gives me total piece of mind:
- all ivars suffixed with _II
- all methods, including accessors include _II prior to first argument
(e.g. color_II, setColor_II:, getColor_II:atRow:atColumn:
- all custom bindings suffixed with _II
- all keys prefixed with II (e.g. for use with dictionaries, NSCoding
protocol)
This was all done in addition to stuff I was already doing previously:
prefix of II for all class names, structs, enums.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden