views:

1445

answers:

1

When I make an NSArray using +[NSArray arrayWithObjects:], does it copy those objects? If I release the objects after adding them to the array, will I run into problems?

+10  A: 

No, it doesn't copy them. It retains them. Yes, you can safely release the objects after adding them to the array.

The docs, as always, spell this out very clearly:

Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated. If you want a collection with different object ownership semantics, consider using CFArray Reference, NSPointerArray, or NSHashTable instead.

Jim Puls
Great answer. Also remember that +arrayWithObjects: returns an unowned ("autoreleased") array, so you must retain it to avoid losing it, but the object in the array will be around as long as the array is.
Quinn Taylor
Thanks a lot! You should know that I search the documentation before posting on Stack Overflow (sometimes, I don't catch everything, though).Thanks for taking the time to answer my question.
Jonathan Sterling