Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I don't quite understand what you are trying to say.
I don't have any object for this. I just want to make a simple array of images.
What is the file where you are trying to create the array of objects? Is it a file like myViewController.m? Is the method inside an @implementation... @end block?
If so, you are writing instance methods for some object. Just about all the code you write in Cocoa is instance methods for different objects in your project.
In order to create something like an array that can be shared between different methods, you have to create the array as an instance variable of the object where you want to use it.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
This will free the memory through the auto-release pool. If the caller needs to maintain a reference to the returned result they would need to user the retain method.
You could also call the convenience constructor on NSArrray - arrayWithObjects to achieve the same result.
Warp is right though; the images will never get released this way. If that's a problem, better to make it an instance variable (declared in the .h, set in the init method) like Duncan said.
Do not autorelease the array in this case; that pretty much guarantees it'll be gone by the time you go to use it.
This will free the memory through the auto-release pool.
That's a quite heavy way of doing it. Memory allocation is a relatively heavy operation, and the 'imageNamed' method of UIImage is quite a heavy operation. Given that the contents of the array will always be the same, it feels a complete waste of time to allocate all those objects and run all those 'imageNamed' methods every time you need to access the array. It will also potentially worsen memory fragmentation.
That's a quite heavy way of doing it. Memory allocation is a relatively heavy operation, and the 'imageNamed' method of UIImage is quite a heavy operation. Given that the contents of the array will always be the same, it feels a complete waste of time to allocate all those objects and run all those 'imageNamed' methods every time you need to access the array. It will also potentially worsen memory fragmentation.
Whoops - the static did not register when I looked at the original post. I agree that using the autorelease pool in this case would not be the best approach - thanks for pointing that out.