Reputation: 1
The method init in most classes does a minimal initialization, because if you need more complexity, more has-a related behaviors, then you need to use an initWith method. So why would anybody call an initWith from within the init method? I have seen this, it's even mentioned on Apple's website, but it seems to conceal behaviors that should be explicitly named.
Upvotes: 0
Views: 136
Reputation: 19892
Objects have what is called a "designated initializer", which are (there can be multiple) the init...
methods that should completely initialize and configure the object for use.
Using a UIView
as an example, it's designated initializer is initWithFrame:CGRect
. This initializes the view with a frame (duh).
If init
was the designated initializer, it would still have to define a frame, probably CGRectZero
, and initWithFrame:
would have to call init
and then setFrame:
, which means setting the frame twice. We don't like doing things twice when they can be done once.
That's why in many cases init
is used instead as a shortcut when the properties in the other initializers can have a default value, usually some kind of zero. In the case of CGRect
, CGRectZero
is the zero-value.
Also, since frame
is an important property of UIView
, if your designated initializer was init
, you'd see a lot of this:
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, 20, 20)
It just makes sense to combine those two lines into one, that's why initWithFrame:
is the designated initializer.
Upvotes: 4