Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

czeluff

macrumors 6502
Original poster
Oct 23, 2006
272
2
Hello to all,

i'm still very new at learning Obj-C and have a question. First, take a look at the NSDictionary reference: http://developer.apple.com/document...s/NSDictionary_Class/Reference/Reference.html

Scroll down to Tasks, and youll see two types of methods: "Creating" an NSDictionary and "Initializaing" an NSDictionary. Both have pretty similar methods. However, I know that the "creating" methods interact with your Autorelease pool, and so you don't have to "release" them; you can simply do [pool drain] at the end of the program.


My question is, why not ALWAYS use the "create" methods? Why would I ever want to "initialize" the object, and have to worry about manually releasing that object separately from the NSAutoreleasePool?
 
I don't know why Apple makes a distinction there. Both set of methods do the same thing, but the "creating" methods return an autoreleased object. Autoreleased objects get released when the autorelease pool is released, which happens at a later date. So if you want to keep around an object you created (for example, you create it in an init method and reference it later), use the "initializing" methods, or retain the result of the "creating" methods.
 
If you have a long running program you would not want to wait until the end to release all the objects, it would take up too much memory. If it is a GUI type app you are doing, and you use one of the templates in Xcode, it will drain the pool at the end of each event loop, in which case yes it's usually easier to use the factory methods (as they are known) instead of the initializers.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.