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

Spike099

macrumors regular
Original poster
Feb 18, 2007
143
0
Canada
Basically my question is should I ever use [NSArray array]? From my understanding, you do not have to release the memory. But how does the array know when you are finished with it? Should I be autoreleasing arrays initialized in this fashion?
 

phjo

macrumors regular
Jan 8, 2008
149
1
Short answer : never...

It would not make much sense (to create an empty immutable array...), but [NSMutableArray array] would...

Now, to answer your question, [NSMutableArray array] would give an autoreleased object, so you don't want to autorelease it again...

But you could add a few objects and return it in a method, and the caller would then be responsible for retaining it.

phjo
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
This is a convenience constructor. They are already autoreleased, so you don't need to do that (in fact, don't do this). You never need to use them but they can be...well...convenient. It's equivalent to writing NSArray *myArray = [[[NSArray alloc] init] autorelease];

It'll get released when the current autorelease pool does, which could be at some later time. If you want the object to persist beyond the current scope you must assign it to an instance holder variable and send it a retain message.
 

Spike099

macrumors regular
Original poster
Feb 18, 2007
143
0
Canada
Thanks for the replies. I was actually using a mutable array in my project, but used an immutable for the example. Looks like I won't be using it, I'm trying to stay away from autoreleasing.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.