At 19:33 +0100 18/11/08, Marc Stibane wrote:
what's the reason for defining a local variable aViewController to receive the UIViewController pointer, then copying that to the instance variable with a setter method which increases the retain count, then decrease the retain count again - instead of just using the instance variable?

  viewController = [[UIViewController alloc]
             initWithNibName:@"MoveMeView" bundle:[NSBundle mainBundle]];

since "viewController" is a member of the class, you don't need the "self.".


viewController = xxx simply assigns the ivar.

self.viewController = xxx is actually syntactic sugar for

[self setViewController:xxx]

which means bindings and observing stuff might be happening, as well as releasing old versions.

I would actually write:

self.viewController = [[[UIViewController alloc]
  initWithNibName:@"MoveMeView" bundle:[NSBundle mainBundle]] autorelease];

The up side of this is:

* less code than the original
* setter is classed
* memory retention is left to the setter
* any old value of viewController is released as necessary
* autorelease is a good safe paradigm

The downside compared to the original:

* slightly less efficient because of the autorelease call

The downside compared to the ivar assignment version:

* Somewhat less efficient because of the autorelease and the setter method call

However, in this case it is a view allocation, which will be far more expensive that the autorelease or the setter, and presumably very infrequently executed, so the advantages in safe, clean, clear, simple, short code easily outweigh the efficiency issues.

IMO of course. The original method will work equally well and si more efficient - the only downside is the extra lines of code. The ivar setting method is much more risky - changes far away from this code could introduce bugs.

Enjoy,
   Peter.


--
              Keyboard Maestro 3 Now Available!
                Now With Status Menu triggers!

Keyboard Maestro <http://www.keyboardmaestro.com/> Macros for your Mac
<http://www.stairways.com/>           <http://download.stairways.com/>
_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to