What is the purpose of having multiple initializers?


#1

Hey guys,

I understand the purpose of having an initializer is to set the instance variables to certain values. However, I am confused as to why you would want to have multiple initializers for each instance variable. Can’t one initializer set all the instance variables? I guess my question really is what is the practicality of having multiple initializers in a class. For instance the book has the following:

- (id)initWithItemName:(NSString *)name { return [self initWithItemName:name valueInDollars:0 serialNumber:@""]; }

The designated initializer already initializes the itemName, so to me, the code up above seems entirely redundant. Can somebody please clarify the purpose of having these multiple initializers.

Thanks guys,
Jack


#2

Suppose you are confronted with a designated initializer that takes multiple arguments.

Now, pose the following question.

What if I don’t want to set all the arguments? I want to set only the most crucial ones, and I want reasonable defaults for those that I don’t set.

What to do? Call the convenience initializers to come to the rescue.


#3

According to Apple’s Documentation:

Source: developer.apple.com/library/ios … ation.html