initWithCoder: shouldn't it just call the designated init?


#1

Hi,

I have a question about this method in this chapter. Inside this method, self = [super init] is called.

The thing is, there is another designated init is implement (- (id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber)

shouldn’t we modify this init method a bit, so that it accepts datecreated and imagekey?

My concern is, shouldn’t a class has only 1 designated init method? if so, why are we trying to implement another one in initWithCoder: ? Why don’t we make use of the one that we had, and just modify it a bit?

please advise :slight_smile:


#2

The BNRItem class does only have one designated initialiser: [color=#008000]initWithItemName:valueInDollars:serialNumber[/color]

The dateCreated is declared as readonly, so it has no setter, and hence the reason why it is not a parameter of the initialiser (this is explained in the “Initialisers” section of Chapter 2).

As for [color=#008000]initWithCoder:[/color], you don’t actually send a message to it implicitly, it is only sent a message when the object is archived, and as stated “Classes whose instances need to be archived and unarchived must conform to the NSCoding protocol and implement its two required methods,[color=#008000] encodeWithCoder:[/color] and [color=#008000]initWithCoder:[/color].”

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#3

You could call initWithCoder: a poorly named method, since it doesn’t really belong to the initializer chain. But then again, you could call it a well-named method because it is the first message sent to an object after it has been allocated (during the unarchiving process).

Either way, it doesn’t fit into the designated initializer pattern, so we just treat it like the crazy uncle in the family and ignore it during family photo time.