Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know which initializer is the designated initializer?

Tags:

cocoa-touch

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.

like image 267
lampShade Avatar asked May 27 '11 00:05

lampShade


People also ask

What is a designated initializer?

A designated initializer, or designator, points out a particular element to be initialized. A designator list is a comma-separated list of one or more designators. A designator list followed by an equal sign constitutes a designation.

What is the difference between convenience and designated initializer?

A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up to the superclass chain. Convenience initializers are secondary, supporting initializers for a class.

How many types of Initializers are there in Swift?

Swift defines two kinds of initializers for class types to help ensure all stored properties receive an initial value. These are known as designated initializers and convenience initializers.

What is the name of the initializer in a class declaration in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer.


1 Answers

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.

like image 55
jscs Avatar answered Oct 03 '22 05:10

jscs