Swift init(), Once and For All

Complete guide on initializers in Swift: memberwise, designated, convenience, failable init, required, enum, and more. Learn to use them correctly and avoid errors.

sábado, 22 de marzo de 2025 • 2 min read • Q2BSTUDIO Team

Software-Apps-ArtificialIntelligence-Company

Hi! My name is Kiryl Famin, and I am an iOS developer.

Today I want to deeply analyze a seemingly simple topic: initializers in Swift. Despite its apparent simplicity, a lack of complete understanding of the topic can lead to frustrating errors that are often quickly patched without really understanding the cause.

In this article, we will cover everything related to initializers, including:

  • How to keep a struct's memberwise initializer while defining a custom one
  • Why it is not always necessary to define an initializer in classes
  • Why calling super.init is not always mandatory in a designated initializer
  • Why all fields of a subclass must be populated before calling super.init
  • How to access all parent class initializers with the fewest possible overrides
  • When a required initializer is truly necessary
  • Why UIView.init() is always called without parameters, but init(frame:) and init(coder:) are overridden

At Q2BSTUDIO, we are a development and technology services company specialized in creating custom solutions for various platforms. We are passionate about sharing knowledge with the community and continuously improving the development experience with modern technologies like Swift.

Now, let's move step by step to thoroughly understand this topic.

Summary in English:

  • All fields must be populated in an initializer.
  • Optional var properties have a default value of nil.
  • Structs automatically receive a memberwise initializer.
  • The memberwise initializer disappears when a custom one is defined.
  • A designated initializer ensures all fields are initialized and calls super.init().
  • A convenience initializer simplifies the process by calling another designated initializer.
  • Convenience initializers always call self.init, while designated ones call super.init.
  • If a subclass defines new properties, it loses the convenience initializers of its superclass.
  • To restore a superclass's convenience initializers, it is necessary to override all designated initializers.
  • To minimize the number of overrides, overridden convenience initializers can be used.
  • If a subclass does not introduce new parameters, it inherits all initializers from its superclass.
  • If a superclass only has init() without parameters, it is called automatically in subclasses.
  • A required initializer ensures subclasses implement it, useful in generics, protocols, and calls to Self().
  • UIView.init() internally calls init(frame:) or init(coder:).
  • A failable initializer returns an optional value.
  • Enumerations with raw values automatically get an init?(rawValue:) initializer.
  • All initializers in an enumeration must assign a value to self.

We hope this article has been useful to you. At Q2BSTUDIO, we believe in development excellence and sharing knowledge to help other developers improve their skills every day.

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.