avatarSudha Chandran B C

Summarize

Initializers in Swift — Part 1/3 (Structures and enumerations)

Photo by Nordwood Themes by Unsplash

Initializers in swift is a very important and vast topic. So would like to write it in 3 parts containing introduction, overview in one part and initializers for class in a separate part as it is huge and failable initializers and closures in the initializers in the last part.

This write up will be mostly important points written as a notes so that you can use it while revising the concept.

Let’s get started..

Introduction

  • 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’s required before the new instance is ready for use.
  • When you create an object in Swift, a special method known as its initializer with init keyword is called.

Initializers:

There are different types of initializers:

  • Default initializers for classes, structures and enumerations.
  • Memberwise initializers for structure types.
  • Designated and Convenience for Classes.
  • Failable initializers for class, structure and enumerations.

We will discuss more about default and initializers related to structures in this part.

Default Initializers:

The default initializer simply creates a new instance with all of its properties set to their default values.

class ShoppingListItem {
    var name: String?
    var quantity = 1
    var purchased = false
}
var item = ShoppingListItem()

Because all properties of the ShoppingListItem class have default values, and because it’s a base class with no superclass, ShoppingListItem automatically gains a default initializer implementation that creates a new instance with all of its properties set to their default values.

Memberwise Initializers for Structure Types:

  • Structure types automatically receive a memberwise initializer if they don’t define any of their own custom initializers.
  • Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that don’t have default values.
struct Size {
    var width = 0.0, height = 0.0
}
let twoByTwo = Size(width: 2.0, height: 2.0)
let zeroByTwo = Size(height: 2.0)
print(zeroByTwo.width, zeroByTwo.height)
// Prints "0.0 2.0"
let zeroByZero = Size()
print(zeroByZero.width, zeroByZero.height)
// Prints "0.0 0.0"

Initializer Delegation for Value Types

  • Initializers can call other initializers to perform part of an instance’s initialization.
  • This process, known as initializer delegation, avoids duplicating code across multiple initializers.
  • Value types (structures and enumerations) don’t support inheritance, and so their initializer delegation process is relatively simple, because they can only delegate to another initializer that they provide themselves.
  • Classes, however, can inherit from other classes. This means that classes have additional responsibilities for ensuring that all stored properties they inherit are assigned a suitable value during initialization.
  • For value types, you use self.init to refer to other initializers from the same value type when writing your own custom initializers. You can call self.init only from within an initializer.
  • Example

Failable Initializers

  • To cope with initialization conditions that can fail, define one or more failable initializers as part of a class, structure, or enumeration definition.
  • You write a failable initializer by placing a question mark after the init keyword (init?).

You can’t define a failable and a nonfailable initializer with the same parameter types and names.

  • A failable initializer creates an optional value of the type it initializes. You write return nil within a failable initializer to indicate a point at which initialization failure can be triggered.

Strictly speaking, initializers don’t return a value. Rather, their role is to ensure that self is fully and correctly initialized by the time that initialization ends.

Although you write return nil to trigger an initialization failure, you don’t use the return keyword to indicate initialization success.

  • Example

Failable Initializers for Enumerations

  • You can use a failable initializer to select an appropriate enumeration case based on one or more parameters.
  • The initializer can then fail if the provided parameters don’t match an appropriate enumeration case.
  • Enumerations with raw values automatically receive a failable initializer, init?(rawValue:), that takes a parameter called rawValue of the appropriate raw-value type and selects a matching enumeration case if one is found, or triggers an initialization failure if no matching value exists.
  • Example

So, here you’ve read about how can we create initializers in Structures and enumerations.

Check about initializers in classes in next part of the series.

Thank you for reading my article!

iOS
Swift
Initialization
Initializer
Basics
Recommended from ReadMedium