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 default initializer is a parameterless initializer (init()) automatically synthesized by the Swift compiler for any class or structure that provides default values for all of its stored properties and does not explicitly define any designated initializers. It instantiates a type by assigning every property its predefined default value.

Conditions for Synthesis

The compiler generates a default initializer only if all of the following criteria are met:
  1. The type is a class or a struct.
  2. Every stored property within the type has an explicitly assigned default value (or is an optional type, which implicitly defaults to nil).
  3. There are no custom designated initializers defined within the primary declaration of the type.

Syntax and Behavior

When the conditions are met, the compiler implicitly generates an init() method.
struct ServerConfig {
    var port: Int = 8080
    var host: String = "localhost"
    var isSecure: Bool = false
}

// The default initializer init() is automatically available
let config = ServerConfig()
The compiler effectively synthesizes the following implementation behind the scenes:
// Implicitly generated by the compiler
init() {
    // Properties are initialized to their default values
}

Access Control Rules

A synthesized default initializer generally receives the same access level as the type it initializes. However, there is a strict exception for public (and open) types: the synthesized default initializer is assigned an internal access level. To instantiate a public type from another module using a parameterless initializer, you must explicitly define a public init().
public struct PublicConfig {
    public var timeout: Int = 30
    
    // The synthesized init() is internal. 
    // This explicit declaration is required for cross-module instantiation.
    public init() {} 
}

Suppression and Retention Rules

Loss of Synthesis If you define any custom designated initializer within the primary body of the type, the compiler immediately suppresses the generation of the default initializer.
struct DatabaseConnection {
    var timeout: Int = 30
    
    // Defining this custom initializer removes the default init()
    init(timeout: Int) {
        self.timeout = timeout
    }
}

// ERROR: Missing argument for parameter 'timeout' in call
// let db = DatabaseConnection() 
Retaining the Default Initializer (Value Types Only) For value types (struct), you can define custom initializers without forfeiting the synthesized default initializer by declaring the custom initializers inside an extension rather than the primary type declaration.
struct CacheSettings {
    var capacity: Int = 100
}

// Custom initializer defined in an extension
extension CacheSettings {
    init(capacityMultiplier: Int) {
        self.capacity = 100 * capacityMultiplier
    }
}

// Both initializers are now available
let defaultCache = CacheSettings() 
let customCache = CacheSettings(capacityMultiplier: 2)
Note on Classes: This extension retention rule applies exclusively to value types. In Swift, designated initializers for a class cannot be declared in an extension. Class extensions can only contain convenience initializers, which are strictly required to delegate to an existing designated initializer.

Class Inheritance Considerations

Subclasses do not synthesize their own default initializers. Instead, they rely on Swift’s Automatic Initializer Inheritance rules. For a subclass to expose a parameterless init(), it must inherit it from its superclass. This inheritance occurs automatically if the subclass meets the following conditions:
  1. It provides default values for all newly introduced stored properties.
  2. It does not define any designated initializers of its own.
Under these conditions, the subclass inherits all designated initializers from its superclass. If the superclass possesses a parameterless init() (whether explicitly defined or synthesized), the subclass inherits that parameterless initializer.
Master Swift with Deep Grasping Methodology!Learn More