Re: release and reference counting query
Re: release and reference counting query
- Subject: Re: release and reference counting query
- From: mmalcolm crawford <email@hidden>
- Date: Wed, 19 Nov 2008 01:05:59 -0800
On Nov 18, 2008, at 10:33 AM, Marc Stibane wrote:
Lets forget for a moment that the dealloc never get's called at all
on the iPhone
This is simply untrue.
viewController = [[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle
mainBundle]];
since "viewController" is a member of the class, you don't need the
"self.".
In the original case you *do* need "self." to ensure that the accessor
method is invoked.
So why the local var?
3 lines of code instead of 1...
Isn't a main goal of Cocoa to write *less* code?
Because:
Cocoa strongly advocates you use accessor methods to set instance
variables(*);
You are discouraged from setting instance variables directly anywhere
other than in initializers and dealloc(*);
On iPhone in particular, you should avoid the use of autorelease.
The latter point in particular means that this alternative:
self.viewController = [[[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle
mainBundle]] autorelease];
does not follow best practice.
Thus -- almost by a process of elimination -- you're left with the
pattern shown in iPhone samples.
mmalc
(*) Same combined point for both:
You need to take account of memory management semantics, which might
potentially change over time -- your example should be:
[viewController release];
viewController = [[UIViewController alloc]
initWithNibName:@"MoveMeView" bundle:[NSBundle
mainBundle]];
Moreover, the accessor might have side-effects that you're then
sidestepping (this is particularly true for KVO/bindings).
_______________________________________________
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