0

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.

JDD
  • 1
  • 2

1 Answers1

4

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.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • Except that these days the frame is not set until much later. – JDD May 16 '18 at 17:29
  • 2
    @JDD What do you mean? If you call `[[UIView alloc] init]`, it definitely initializes `frame`. – Rob Napier May 16 '18 at 17:48
  • 2
    And just double-checked in case something had been changed, but `-[UIView init]` calls `-[UIView initWithFrame:CGRectZero]` like it always has. Is there something we're missing? `init` doesn't do "minimal initialization." It does complete initialization (all `init` methods do). It just provides more default values. – Rob Napier May 16 '18 at 17:54
  • Yikes I detect a smell. Best practice is for the method name to be a clear and complete representation of what the method does. If Obj-C's designers thought this was a good idea, they were in error. Imagine if init also set the background to blue, but "init" in and of itself doesn't indicate that. – JDD May 16 '18 at 18:27
  • Huh? The whole point of a designated initializer -- which should always be called -- is to provide a set of default values that are universal. If the author's of UIView had decided the default background should be blue, then that should be set when calling through -init (which calls through to the actual designated initializer). – bbum May 16 '18 at 19:14
  • Best practice is for the method name to be a clear and complete representation of what the method does. "init" is vague, incomplete, unclear. – JDD May 17 '18 at 20:59
  • `init` initializes the object in it's most basic state, what else do you need to know from it's name? It seems like you've already made up your mind and just wanted us to tell you what you already think. I don't get the point of you asking here if you're not accepting the answers we've given you. Look at the profiles of the people who replied to you, we know what we're talking about. – EmilioPelaez May 18 '18 at 14:29