Initialization in Swift

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This includes setting initial values for properties and performing any necessary setup.

In Swift, initializers are special methods invoked to prepare an instance. Unlike Objective-C, Swift initializers don’t return a value. Their main purpose is to ensure instances are correctly initialized.

A simple initializer in Swift looks like this:

Class instances can also have a deinitializer to perform cleanup before deallocation.

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.

Syntax

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

OR

Or if with our ViewControllers

 

Convenience Initializers

Convenience initializers serve as secondary or supporting initializers for a class. While not mandatory, they enhance flexibility and can simplify the process of instance creation. Here’s what you need to know:

  • Purpose: They are shortcuts for common initialization patterns, making it easier to initialize objects for specific use-cases.
  • Designation: Use the convenience keyword before the init method to define one.
  • Requirement: They must call a designated initializer from the same class.
  • Advantage: While optional, they expand your options, allowing for more versatile and readable initializations.

For example, if you often create an instance with a particular set of default values, a convenience initializer can encapsulate those defaults, reducing repetitive code.

Syntax

Basic Example

Real-World Example

This from the Apple Docs is another good demonstration:

Recap on Initializers in Swift

  1. Designated Initializers: Must call a designated initializer from its immediate superclass.
  2. Convenience Initializers:
    • Must call another initializer from the same class.
    • Ultimately, they should lead to a designated initializer being called.

Automatic Initializer Inheritance

Not all superclass initializers are inherited by subclasses. However, there are specific conditions under which this inheritance occurs automatically:

  • Rule 1: If a subclass doesn’t define any designated initializers and provides default values for any new properties it introduces, it will inherit all of its superclass’s designated initializers.
  • Rule 2: If a subclass provides implementations for all of its superclass’s designated initializers (either by inheriting as per Rule 1 or by defining custom implementations), it will also inherit all of the superclass’s convenience initializers.

Reference

MORE INFORMATION ON INITIALIZERS

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: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

Some Syntactic Sugar

Further Illustration (Source: Apple Docs)

 

References

A: https://docs.swift.org/swift-book/LanguageGuide/Initialization.html

B: https://useyourloaf.com/blog/adding-swift-convenience-initializers/

C: https://www.raywenderlich.com/1220-swift-tutorial-initialization-in-depth-part-1-2

D: https://www.raywenderlich.com/1219-swift-tutorial-initialization-in-depth-part-2-2

E: https://www.tutorialspoint.com/swift/swift_initialization

F: https://www.journaldev.com/16889/swift-init

G: https://docstore.mik.ua/orelly/java-ent/jnut/ch03_02.htm

H: https://www.programiz.com/kotlin-programming/constructors

I: https://www.tutorialsteacher.com/csharp/csharp-object-initializer

J: Swift Notes for Professionals

K: https://medium.com/@abhimuralidharan/initializers-in-swift-part-1-intro-convenience-and-designated-intializers-9adf5632fb52

Loading

Last modified: September 27, 2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.