• 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: newbie question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie question


  • Subject: Re: newbie question
  • From: "Stephen Deken" <email@hidden>
  • Date: Fri, 10 Nov 2006 11:02:28 -0600

On 11/10/06, Alan Smith <email@hidden> wrote:
One thing I didn't realize for quite some time when I was first
learning how to use the Cocoa frameworks is that all methods that have
the plus before them, should, return an autoreleased instance.

No, no, no! +[alloc] famously does not return an autoreleased instance. The 'methods with a plus' are just class methods, and they are free to return whatever they'd like, autoreleased or not.

Autoreleased objects are not something you should use a lot of because
they don't get released until your program quits, or sooner if you're
controlling the pool and specifically release it.

This is misleading. AppKit creates and releases autorelease pools all the time -- each pass through the runloop for sure and probably more often than that in practice. As long as you don't start creating thousands upon thousands of objects in a single method, you're probably ok. But you do *not* have to specifically control and release the pool in order to use autoreleased objects, and autoreleased objects do *not* persist until program shutdown in most cases.

I prefer: NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; ... [dict release];
Over: NSMutableDictionary *dict = [NSMutableDictionary dictionary];
Keeping your memory clean makes your program faster and look better.

Agreed, with caveats -- using [dictionaryWith...] is helpful here:

NSArray *arr = [[NSArray alloc] initWithObjects:
 [NSDictionary dictionaryWith...],
 [NSDictionary dictionaryWith...],
 [NSDictionary dictionaryWith...],
 [NSDictionary dictionaryWith...],
 nil];

Using -[dictionaryWith...] and letting the autorelease pool release
them (harmlessly, since they are retained by the array) makes the code
easier to read, and the impact on execution time is negligible.

Stephen Deken
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Re: newbie question
      • From: "Alan Smith" <email@hidden>
References: 
 >newbie question (From: maximilian marcoll <email@hidden>)
 >Re: newbie question (From: "Alan Smith" <email@hidden>)

  • Prev by Date: Re: newbie question
  • Next by Date: Re: Re: newbie question
  • Previous by thread: Re: newbie question
  • Next by thread: Re: Re: newbie question
  • Index(es):
    • Date
    • Thread