> ## 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.

# Swift Default Initializer

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.

```swift theme={"dark"}
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:

```swift theme={"dark"}
// 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()`.

```swift theme={"dark"}
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.

```swift theme={"dark"}
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.

```swift theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
