• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: release and reference counting query
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: release and reference counting query
      • From: Marc Stibane <email@hidden>
References: 
 >release and reference counting query (From: Calum Robertson <email@hidden>)
 >Re: release and reference counting query (From: Roland King <email@hidden>)
 >Re: release and reference counting query (From: Marc Stibane <email@hidden>)

  • Prev by Date: Re: Outlets / IBOutlet declarations (was Re: Interface Builder & Wiring Objects)
  • Next by Date: Re: Binding a CoreData string value to an NSTextView
  • Previous by thread: Re: release and reference counting query
  • Next by thread: Re: release and reference counting query
  • Index(es):
    • Date
    • Thread