Cristian Contreras
Cristian Contreras

Reputation: 21

What is a convenience initializer as it pertains to Objective-C?

I started learning programming about 8 months ago, started with C, OOP, now onto iOS, which is my goal. Everything is going pretty smooth for the most part and I've started to practice by programming small applications on xcode. It's just little terms like convenience initializer that sometimes throw me off. Can anyone define this term for me and just give me a quick example of its usage? I understand the concept of allocation and initialization but I am unable to differentiate between an initializer and a convenience initializer. I've looked online but not much luck.

Any help is appreciated, thanks

Upvotes: 1

Views: 1359

Answers (2)

Cyanhall
Cyanhall

Reputation: 81

I had the same doubt, but solved now: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MultipleInitializers.html

Note: if the above link failed, read this backup: https://gist.github.com/JeOam/9116926

Upvotes: 0

Macmade
Macmade

Reputation: 53970

Basically, a convenience initializer/constructor is a class (static / non member) method, that returns an instance of an class.

It means that, in order to get an actual instance of a class, you may use a convenience constructor (or initializer), if one is provided, rather that allocating the object explicitly.

It will replace the standard alloc/init (or initWith...) way.

But...

In terms of memory management, it has a completely different meaning!

You don't own an object returned by a convenience constructor.
It means you don't have to release it by yourself.

For instance, when you allocate a NSMutableArray:

NSMutableArray * a = [ [ NSMutableArray alloc ] initWithCapacity: 10 ];

You called alloc. It means you'll have to release the object, as you own it:

NSMutableArray * a = [ [ NSMutableArray alloc ] initWithCapacity: 10 ];

/* Some stuff here... */

[ a release ];

It's not the case with a convenience constructor, as the object will be released automatically (it's automatically placed on the current auto-release pool):

NSMutableArray * a = [ NSMutableArray arrayWithCapacity: 10 ];

/* Some stuff here... */
/* No need to release the array! */

Basically, here's what the arrayWithCapacity convenience constructor of NSMutableArray does:

+ ( NSMutableArray * )arrayWithCapacity: ( NSUInteger )capacity
{
    return [ [ [ NSMutableArray alloc ] initWithCapacity: capacity ] autorelease ];
}

Note the call to autorelease at the end?
It means the object will be released automatically at the end of the current run loop, so you don't have to do it by yourself (if you don't retain it explicitly, of course).

Final note

You did not mention if you are using ARC or not.
I assumed it's not the case.

Of course, if you use ARC, this is completely different, as the retain/release are done automatically (most of the time), based on variables qualifiers (strong/weak).

But even if using ARC, you should know how reference counting works.

Upvotes: 3

Related Questions