Re: Retain/Release and Properties clarification
Re: Retain/Release and Properties clarification
- Subject: Re: Retain/Release and Properties clarification
- From: Kyle Sluder <email@hidden>
- Date: Mon, 03 Oct 2011 11:31:33 -0700
On Mon, Oct 3, 2011 at 11:23 AM, Charles Srstka
<email@hidden> wrote:
> 1. Apple reserves the underscore prefix for their own use, so you could, at least theoretically, clash with a superclass ivar this way, and
[snip]
>
> 3. If I use an ivar prefix that no one else uses (as far as I know), then I can make my class into a subclass of something from a publicly available framework without needing to worry about ivar name clashes.
This only matters at compile time. If you have a class that subclasses
a framework class, and you add an ivar named "_foo", later releases of
the OS can add an ivar named "_foo" and your application will continue
to run on the latest release without issue. (Well, on 32-bit Mac OS X,
they can rename an existing ivar to "_foo" without issue; adding an
ivar will trigger the Fragile Base Class problem.)
On 32-bit Mac OS X, ivar accesses get translated into an offset.
"self->_foo = 42" gets translated into something like "*(self + 18) =
42". On 64-bit Mac OS X and on iOS, ivar access is done indirectly via
symbol. "self->_foo = 42" gets translated into something like "*(self
+ OBJC_IVAR_$_MyClass__foo) = 18". Neither of these methods actually
cares what the ivar is named (the compiler could generate offset
symbols based on the days of the week if it wanted to).
>
> 2. KVO’s “access instance variables directly” (mis)feature recognizes the underscore prefix. I like to give it a prefix that KVO doesn’t know about so that I can be sure never to end up accidentally accessing the ivars of another object without going through the proper accessors.
This is indeed a good reason to avoid using plain underscores. I hate
this misfeature so much.
--Kyle Sluder
_______________________________________________
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