• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Q: Designated initializers: what are they?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Q: Designated initializers: what are they?


  • Subject: Re: Q: Designated initializers: what are they?
  • From: Ali Ozer <email@hidden>
  • Date: Sat, 12 May 2001 10:14:59 -0700

> I wrote a custom NSView for which I wanted to implement drag and drop.
> It failed to receive drops because it turned out that the object never
> received the registerForDraggedTypes: message I was sending it from my
> initWithFrame: method, because this method was never called. Instead
> initWithCoder: was called, which I did not think to implement.
>
> Now, the reason for my confusion is that initWithFrame: is supposed to
> be the "designated initializer" for the NSView class. Doesn't that mean
> that that's the one method that's always going to be called eventually,
> no matter what other initializers get called? Since that's (apparently)
> not the case, what does it mean to be a "designated initializer"? And
> am I supposed to duplicate my initialization code in every possible
> initializer, i.e. initWithFrame: and initWithCode:, or is there a more
> elegant solution?

There can be multiple designated initializers. It turns out that
initWithCoder: is also an (implicit?) designated initializer for classes
which archive. Thus, classes which override the DI init... methods would
need to override initWithCoder: as well if they want to unarchive
themselves.

So one solution is to call your common initialization from both
initWithCoder: and initWithFrame:. If you're concerned about
initWithCoder: just because of IB unarchiving (which is almost always
the case for custom views; you normally do not archive them otherwise),
then as Henri says you can use awakeFromNib instead of overriding
initWithCoder:.

There is a decent designated initializer discussion at

http://developer.apple.com/techpubs/macosx/Cocoa/Reference/Foundation/ObjC_classic/
Classes/NSObject.html#//apple_ref/occ/instm/NSObject/init

but it doesn't go into the initWithCoder: aspect.

Ali


  • Follow-Ups:
    • Re: Q: Designated initializers: what are they?
      • From: Ali Ozer <email@hidden>
References: 
 >Q: Designated initializers: what are they? (From: email@hidden)

  • Prev by Date: Re: Q: Designated initializers: what are they?
  • Next by Date: Re: Q: Designated initializers: what are they?
  • Previous by thread: Re: Q: Designated initializers: what are they?
  • Next by thread: Re: Q: Designated initializers: what are they?
  • Index(es):
    • Date
    • Thread