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 struct (structure) in Swift is a custom, composite data type that encapsulates related properties and methods. It operates as a value type, meaning its instances are copied upon assignment or when passed as arguments to functions, ensuring independent state across different execution scopes.
struct StructName {
    var mutableProperty: Int
    let immutableProperty: String

    // The compiler automatically synthesizes a memberwise initializer:
    // init(mutableProperty: Int, immutableProperty: String)

    func computeValue() -> Int {
        return mutableProperty * 2
    }

    mutating func updateState(newValue: Int) {
        mutableProperty = newValue
    }
}

// Defining a custom initializer in an extension preserves the synthesized memberwise initializer
extension StructName {
    init(defaultString: String) {
        self.mutableProperty = 0
        self.immutableProperty = defaultString
    }
}

Core Technical Characteristics

Value Semantics Because structs are value types, assigning a struct to a new variable or passing it to a function creates a distinct copy in memory. Modifications to the copy do not affect the original instance. Standard library collections in Swift (such as Array, Dictionary, and String) are implemented as structs and utilize a copy-on-write (COW) optimization to defer actual memory duplication until a mutation occurs. Memory Allocation Structs are typically allocated on the stack rather than the heap. This avoids the overhead of Automatic Reference Counting (ARC) and dynamic memory allocation, resulting in faster instantiation and deallocation compared to reference types (classes). Synthesized Memberwise Initializers If custom initializers are not explicitly defined within the main struct body, the Swift compiler automatically synthesizes a memberwise initializer. This initializer exposes parameters for all stored properties, allowing for immediate initialization without boilerplate code. Crucially, if a developer defines a custom initializer inside an extension rather than the main struct declaration, the struct retains its automatically synthesized memberwise initializer alongside the custom one. Immutability and the mutating Keyword The mutability of a struct’s properties is strictly governed by the instance’s declaration. If a struct instance is assigned to a constant (let), all of its properties become immutable, regardless of whether they were declared as var within the struct definition. Any instance method that modifies the struct’s internal state must be explicitly prefixed with the mutating keyword. This keyword signals to the compiler that the method will alter the underlying value, an action that is strictly prohibited on constant instances. Inheritance, Polymorphism, and Type Casting Structs do not support inheritance; a struct cannot inherit properties or methods from another struct or class. Consequently, they lack hierarchical downcasting and deinitializers (deinit). However, Swift fully supports runtime type casting for structs using the is, as?, and as! operators when casting from existentials (such as Any or protocols) to a specific struct type. To achieve polymorphism and shared behavior, structs rely entirely on protocol conformance and protocol extensions rather than classical object-oriented subtyping.
Master Swift with Deep Grasping Methodology!Learn More