In an objective-c class that can be initialized through different init… methods, it is common sense to collect initialization code that is common to all initializers into one common method that is called from the other init* methods (or also sometimes from awakeFromNib).
Is there a convention for how this method should be named? initializer? initCommon? …?
According to Apple, initializer methods should always begin with the word ‘init,’ followed by name components that describe the arguments. If a class has more than one initializer, the methods should be chained together so that only one of them is doing all the work, and the others should simply provide default values for the missing arguments.
So for example, a Person class might have the following init… methods:
UPDATE
As @dreamlax points out, Apple’s documentation recommends (and when compiling with ARC, the compiler requires) reassigning
selfwith the return value from the call to[super init].The docs also recommend checking for
nilbefore performing any further initialization That’s because if the call to[super init]returnsnil,selfwould already have been deallocated by the time the call returns, so there would no longer be an instance to initialize.Apple’s documentation also suggests avoiding calls to accessor methods in
init...methods; instead, they recommend directly setting the instance variables. So theinitWithFirstName:lastName:age:method shown above should ideally be written in a manner similar to the following example: