Dear fellow iOS developers,
I’m still developing without ARC (switching coming soon) and after an interesting discussion with a iOS beginner, I’ve presented my way to initialize a retain property (let’s call it property), that I inherited from Apple docs at their pre-ARC era:
NSObject *tmpProperty = [[NSObject alloc] init];
self.property = tmpProperty;
[tmpProperty release];
I see a great benefit with this: it makes memory management clear.
There’s also a great drawback: it takes 3 lines, for something very basic. So we assumed: why not the following solution?
self.property = [[[NSObject alloc] init] autorelease];
It’s less clear about memory management but much more compact (and it propably make the ARC-migration easier to achieve).
What are (were?) you using yourself? Do you think one of these is a clearly better solution than the other?
Using autorelease is never a great option, as you have no control over the lifetime of the object you are initializing, so it may be the case that
In a ViewController you have initialized some object e.g. property, and you are swtiching to some other views, and if you want to come back to the older ViewController and access the object, it might be released by the iOS, and your app might crash.
I think switching to ARC at the earliest would be your best option. I think ARC should be used by every developer, almost all the devices are being provided by the iOS5 update, so there might be least chances of supporting older devices.
Hope this solves your doubt