What is the best way to initialize an object?

ios, objective-c

Solution

Every class should have a designated initializer; the one true `init...` method variant that must always be called to properly initialize the class (as Matt states).

However, that doesn't fully solve the problem in that a class may also support instantiation through unarchival, which runs into the same "where do I put the common initialization goop" problem in your question.

Typically, I create a method like:

- (void)commonInit
{
     ... common initialization goop goes here ...
}

And then call it first from the designated initializers and/or `initWithCoder:`.

Yes, a bit messy, but not much you can do about it.

A general strategy to reduce the mess:

• provide very very few initializers; maybe `init` + one initializer with all the possible arguments for other configurations.

• Designate the `super`'s initializer as your DI and make any additional initializers call through it. When subclassing where you have a more specific DI (see `UIView`'s `initWithFrame:`), override the super's DI to call `[self initFancy:...]` which then calls `[super initThroughSupersDI]`.

Problem

I've been using Objective-C for a few years, but I'm still unsure how to initialize an object. Our problem is this: ``` -(id)init { self = [super init]; if (self) { self.backgroundColor = [UIColor blueColor]; self.textColor = [UIColor greenColor]; } return self; } -(id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor yellowColor]; } return self; } ``` Getting it to be initialized the same in both methods means either copy and paste coding (huge no-no, causes endless problems due to human error, such as values being different, like in the example above) or a new method: ``` -(id)init { self = [super init]; if (self) { [self initialSetup]; } return self; } -(id)initialSetup { self.backgroundColor = [UIColor blueColor]; self.textColor = [UIColor greenColor]; } ``` This then has the problem that if a subclass has an `initialSetup` method, the superclass calling `[self initialSetup]` will call the subclass's `initialSetup`, and thus ignore all of the superclass's setup. You can get around this by appending the class name each time, such as `initialSetupTextField`. This seems rather messy though. This is all just to initialize an object. How does everyone else do this?

Original source

Related problems