The Designated Initializer (Got the Better of Me)
©2008 James Dempsey
The designated initializer got the better of me
The designated initializer got the better of me
It’s a cautionary tale my friends, I’m sure you will agree
Don’t do what I did do or it will happen to you
Like it happened to me
(spoken) Let’s start at the beginning…
When you alloc, a memory block is allocated with precision
The isa pointer’s assigned, so your instance can find its class’ definition.
All the other ivars are filled up with zeros, thus nil’d, if to objects they refer
And then the instance must wait to be put in a good state
By an object initializer
The method init, or some variant of it, performs the initialization
Each new one devised is parameterized for more options in instance creation
Some time and care should be spent, when you implement init methods for a class that’s home grown.
The superclass gets its shot to init what its got, and then you’re on your own.
(Instrumental)
The designated initializer got the better of me
We haven’t gotten there yet, but no I didn’t forget we’ll come around to it eventually
A class with many inits, use the one that best fits, there’s power in the variety
But you better subclass it right, or be debugging all night
Like what happened to me
Among a class’ inits, there’s at least one that sits as its designated initializer
The method that you anoint serves as a funnel point, its setup logic makes it much wiser
The funnel point, well, it passes to its superclass’s designated favorite son
Though it may sound like a blooper, the rest don’t call super
They call your chosen one
(spoken) And here’s what happened to me…
I wrote a subclass, my initializer set up critical state (critical)
I didn’t override my super’s init methods which was ill-advised (ill-advised)
Anyone calling those init methods doomed the new instance to a crashing fate
My superclass’s code was called, but never mine, my critical state, was not initialized!
The designated initializer got the better of me
Just a subtle catch with dynamic dispatch, that got the better of me
It’s a cautionary tale my friends
I hope now you plainly see
Don’t do what I did do, or it will happen to you
Like it happened to me.
The Designated Initializer (Got the Better of Me) debuted at WWDC 2008.
Vocals: James Dempsey
Guitar: Gordie Freedman
Trumpet: Matt MacPhail
Percussion: Russell Bond
Music and Lyrics: James Dempsey
Recorded and Produced by Russell Bond at The Annex Studios