开发者

How do I know which initializer is the designated initializer?

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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜