Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A required initializer is a class initializer marked with the required modifier, enforcing a strict compiler contract that every direct and indirect subclass must implement that specific initializer.

Syntax and Implementation

To designate an initializer as required, place the required keyword before the init declaration in the superclass. When a subclass implements this initializer, it must also use the required keyword to propagate the requirement down the inheritance hierarchy. When a subclass implements a superclass’s already-required initializer, the override modifier is omitted.
class BaseClass {
    var name: String
    
    required init(name: String) {
        self.name = name
    }
}

class SubClass: BaseClass {
    var age: Int
    
    // 'required' is mandatory. 'override' is omitted.
    required init(name: String) {
        self.age = 0
        super.init(name: name)
    }
}

Inheritance Rules

Subclasses do not always need to explicitly provide an implementation for a required initializer. If a subclass satisfies Swift’s rules for automatic initializer inheritance—specifically, by not defining any new designated initializers of its own—it will implicitly inherit the required initializer from its superclass.
import Foundation

class AnotherSubClass: BaseClass {
    // Inherits required init(name:) automatically 
    // because no new designated initializers are introduced.
    var id: UUID = UUID() 
}

Protocol Conformance

When a class conforms to a protocol that declares an initializer, the compiler mandates that the class implement that initializer with the required modifier. This guarantees that any future subclasses will also fulfill the protocol’s instantiation requirements.
protocol Describable {
    init(description: String)
}

class Item: Describable {
    var description: String
    
    // Must be 'required' to satisfy the protocol for all potential subclasses
    required init(description: String) {
        self.description = description
    }
}
The override and required Combination: If a subclass satisfies a protocol’s initializer requirement by overriding a non-required designated initializer from its superclass, both the required and override modifiers are mandatory.
class SuperItem {
    init(description: String) {
        // Non-required designated initializer
    }
}

class SubItem: SuperItem, Describable {
    var description: String
    
    // Both 'required' and 'override' are mandatory
    required override init(description: String) {
        self.description = description
        super.init(description: description)
    }
}
Exception for Final Classes: If a class is marked with the final modifier, it cannot be subclassed. Therefore, when a final class implements a protocol’s initializer, the required keyword is omitted because the inheritance chain is terminated.
final class FinalItem: Describable {
    var description: String
    
    // 'required' is omitted because the class is final
    init(description: String) {
        self.description = description
    }
}
Master Swift with Deep Grasping Methodology!Learn More