Lazy instantiation in Objective-C/ iPhone development Lazy instantiation in Objective-C/ iPhone development objective-c objective-c

Lazy instantiation in Objective-C/ iPhone development


Stunner did a good job of explaining not needing to alloc init objects that have already been created.

But if it is an object that doesn't exist, where are you going to create it? A very common pattern, which I mention because you mentioned it in your post, is lazy instantiation.

So you want an NSMutableArray property. You could alloc init it in some method before you use it, but then you have to worry about "does that method get called before I need my array?" or "am I going to call it again accidentally and re-initialize it."

So a failsafe place to do it is in the property's getter. It gets called every time you access the property.

.h@property (nonatomic, strong) NSMutableArray* myArray;.m@synthesize myArray = _myArray;- (NSMutableArray*)myArray{    if (!_myArray) {        _myArray = [[NSMutableArray alloc] initWithCapacity:2];    }    return _myArray;}

Every time you access that property, it says, "Does myArray exist? If not, create it. If it does, just return what I have."

Plus an added benefit with this design pattern is you aren't creating resources until you need them, versus creating them all at once, say, when your view controller loads or your app launches, which, depending on the requirements, could take a couple seconds.


The reality is when you do self.myProperty = [[Class alloc] init], you're not initializing your property. Rather, you're initializing an object that you tell your property (which is in fact a pointer) to point to. So if you already have an object that's allocated and initialized, you don't have to alloc/init again and you can do self.myProperty = object;

UI Properties do no start as nil, this is because when you add elements in the interface builder, the view owns the elements that you add and these objects are initialized automatically for you. This means if you're creating IBOutlets and connecting them to some properties, you don't have to alloc/init.

I hope this was helpful.


I don't have experience with Storyboards but I know that when you create objects via a xib file all objects are properly instantiated when you tell a view controller to use a xib file. So you need not worry about alloc/initing those objects in code.

Regarding using self.property = <something>, it depends on what something is. If something is any sort of existing object you need not do the alloc init on that object as the self.property = ... syntax calls the property's setter method which will retain, copy, assign, etc. the new value to the property appropriately.

Now any sort of existing object can be an alloc/init'ed object, or an autoreleased object obtained from a convenience method (NSString's stringWithFormat: for example).

As Kaan Dedeoglu pointed out, the self.property = ... syntax points (and retains) the ivar to the object in memory, and it is up to you to initialize that object if it isn't already instantiated.