10

How do I know which initializer is the designated initializer for ANY class? I'm guessing that it is the one that takes the most parameters, but there could be times where this is not correct.

lampShade
  • 4,361
  • 8
  • 34
  • 50
  • What do you mean by 'designated initializer'? init is the standard. – shawnwall May 27 '11 at 01:03
  • 1
    @shawnwall: The designated initializer is the init... method that all other init... methods call. E.g. for `UIView` and `NSView`, this is `initWithFrame:` (it's mentioned in the documentation). See [here](http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-106376) for more details – omz May 27 '11 at 01:06

3 Answers3

6

omz's answer can be stated more firmly: The documentation for a framework class will specify which is the designated initializer. It's necessary to know what the designated initializer is in order to write subclasses that behave properly. The subclass's D.I. must call up to the superclass's D.I. in order to be sure that it is properly initialized.

Your guess about the greatest number of parameters is well-founded, however. Apple actually states that it is often the D.I. in a few places.

Cocoa Core Competencies

The initializer of a class that takes the full complement of initialization parameters is usually the designated initializer.

Cocoa Fundamentals

Some subclasses provide convenience initializers that supply default values to an initializer that takes the full complement of initialization parameters. This initializer is usually the designated initializer, the most important initializer of a class.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • @lampShade: :) As always. Other frameworks than Cocoa _should_ specify the designated initializer if they intend users to subclass. If they don't so specify, get a new framework! Glad to help! – jscs May 27 '11 at 02:30
0

From BigNerdRanch iOS programming book:

*The designated initializer calls the superclass’s designated initializer (directly or indirectly) before doing anything else.

*Any other initializers call the class’s designated initializer (directly or indirectly).

*If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer (directly or indirectly).

Conway, Joe; Hillegass, Aaron; Keur, Christian (2014-02-14). iOS Programming: The Big Nerd Ranch Guide (4th Edition) (Big Nerd Ranch Guides) (Kindle Locations 1906-1910). Pearson Education. Kindle Edition.

dtpdx
  • 1
0

You can't really know this without looking at either the source code (impossible for system frameworks) or the documentation (potentially of a superclass).

omz
  • 53,243
  • 5
  • 129
  • 141