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

A constant in Swift is an immutable identifier bound to a specific value or memory address. Declared using the `let` keyword, a constant guarantees that its assigned value cannot be reassigned once initialized within its defining scope.

## Syntax and Declaration

Constants can be declared with explicit type annotations or rely on the Swift compiler's type inference.

```swift theme={"dark"}
// Type inference
let maximumConnections = 50 

// Explicit type annotation
let environmentName: String = "Production"
```

## Initialization Rules

A constant does not need to be assigned a value immediately upon declaration, but it must be initialized exactly once before its first read access. The Swift compiler enforces definite initialization.

```swift theme={"dark"}
let networkSuccess = true
let responseCode: Int

// Deferred initialization based on control flow
if networkSuccess {
    responseCode = 200
} else {
    responseCode = 404
}

// The compiler guarantees responseCode is initialized before this point
print(responseCode) 
```

## Immutability Mechanics: Value vs. Reference Semantics

The behavior of a constant depends strictly on whether the assigned type is a value type or a reference type.

**Value Types (Structs, Enums, Tuples)**
When a constant holds a value type, the immutability is deep. The instance itself and all of its properties become immutable, regardless of whether the internal properties were declared with `var`.

```swift theme={"dark"}
struct Point {
    var x: Int
    var y: Int
}

let origin = Point(x: 0, y: 0)
// origin.x = 10 // Compiler Error: Cannot assign to property: 'origin' is a 'let' constant
```

**Reference Types (Classes, Actors)**
When a constant holds a reference type, the immutability is shallow. The constant strictly protects the reference (the memory address pointing to the instance). You cannot reassign the constant to point to a different instance, but you can mutate the underlying instance's variable properties.

```swift theme={"dark"}
class Session {
    var isActive: Bool = false
}

let currentSession = Session()

// Allowed: Mutating a property of the referenced instance
currentSession.isActive = true 

// Compiler Error: Cannot assign to value: 'currentSession' is a 'let' constant
// currentSession = Session() 
```

## Evaluation Time

Unlike constants in languages like C or C++ (e.g., `#define` or `constexpr`), Swift constants declared with `let` are not restricted to compile-time evaluation. They are evaluated at run-time, meaning a constant can be initialized with the result of a function call or a dynamically calculated expression.

```swift theme={"dark"}
import Foundation

// Run-time evaluation
let currentTimestamp = Date().timeIntervalSince1970
```

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