Initialization means to set to the value, or put in the condition, appropriate to the start of an operation.

In Swift, “Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.

You implement this initialization process by defining initializers, which are like special methods that can be called to create a new instance of a particular type. Unlike Objective-C initializers, Swift initializers do not return a value. Their primary role is to ensure that new instances of a type are correctly initialized before they are used for the first time.

A basic initializer in Swift looks like:

Instances of class types can also implement a deinitializer, which performs any custom cleanup just before an instance of that class is deallocated” (Ref: A).


Designated Initializers

Image Source Ref#: J (originally from Apple Docs)
  • Designated initializers are the primary initializers for a class
  • They have to fully initialize all the properties introduced by that class
  • They must also call a designated initializer from their immediate superclass (then up the chain of classes)
  • Classes will typically have one designated initializer, but can easily have more
  • Every class must have at least one designated initializer.
  • The chain of delegating initializers will end by a call to a non-delegating initializer 

In some cases, this requirement is satisfied by inheriting one or more designated initializers from a superclass because of Automatic Initializer Inheritance.

Designated initializers for classes are written in the same way as simple initializers for value types.


So these are the inits we all used to seeing in our Swift code, such as:


Or if with our ViewControllers

Convenience Initializers

  • secondary, supporting initializers for a class
  • they’ve got to call a designated init from the same class
  • you don’t need these but they can help to widen your options
  • You can define a convenience initializer to create an instance of your class for a particular use-case or input value type.
  • place the convenience word before init to make one of these.
  • these can be shortcuts to common initialization patterns


Basic Example

Real-World Example

This from the Apple Docs is another good demonstration:

To recap then:

  • A Designated Initializer must call a Designated Initializer from its immediate superclass.
  • A Convenience Initializer must call another initializer from the same class.
  • A Convenience Initializer has to ultimately call a designated initializer.

Automatic Initializer Inheritance


“””(Ref #: K).


Throwable Initializers 

Using Error Handling we are able to make a Struct(or a class) initializer into a so-called throwable initializer.

Example Error Handling enum:

You can use Error Handling enum to check the parameter for the Struct(or class) meet expected requirement

Source: Swift Notes for Professionals

Required Initializers

Writing the keyword required before the initializer tells us that each subclass must implement that initializer. Furthermore, using this means required modifier must be present at the respective subclass implementations as well.

This is exactly why we need this bit of code when were using initializer dependency injection as in the above example:

For a more comprehensive breakdown, an ok reference is found here:

Some Syntactic Sugar

Further Illustration (Source: Apple Docs)












J: Swift Notes for Professionals


 2,216 total views,  2 views today

Last modified: August 20, 2022



Write a Reply or Comment

Your email address will not be published.