> ## 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 Typed Variable

A typed variable in Swift is a named, mutable storage location (declared with `var`) in memory strictly bound to a specific data type at compile time. While Swift strictly distinguishes between mutable variables (`var`) and immutable constants (`let`), both constructs enforce the same rigorous static typing rules. Because Swift is a statically typed language, the compiler enforces type safety; once a variable's type is established, it cannot be reassigned to hold data of a different type.

Swift establishes a variable's type through either **Type Annotation** (explicit declaration) or **Type Inference** (compiler deduction based on the initial value).

```swift theme={"dark"}
// Type Annotation: Explicitly defining the type using a colon
var explicitInteger: Int = 42
let explicitString: String = "Literal" // Constant, but follows identical typing rules

// Type Inference: The compiler deduces the type from the assigned value
var inferredDouble = 3.14 // Inferred as Double
var inferredBool = true   // Inferred as Bool
```

## Core Mechanics

**Type Safety and Strictness**
Swift performs rigorous type checking during compilation. Attempting to assign a value of an incompatible type to a typed variable results in a compile-time error. Implicit type conversion (coercion) is not supported. Converting between distinct numeric types requires explicit initialization, whereas navigating class hierarchies or protocols requires explicit type casting operators (`as`, `as?`, `as!`).

```swift theme={"dark"}
var counter: Int = 10
// counter = "Ten" // Compile-time error: Cannot assign value of type 'String' to type 'Int'

// Explicit initialization required to create a Double from an Int
var floatingPoint: Double = Double(counter) 
```

**Initialization Requirements**
A typed variable must be initialized with a value before it is read. If a variable is declared without an initial value, a type annotation is mandatory because the compiler has no value from which to infer the type.

```swift theme={"dark"}
var deferredInitialization: String // Type annotation required
// print(deferredInitialization)   // Compile-time error: Variable used before being initialized
deferredInitialization = "Active"
```

**Optional Typing**
In Swift, standard typed variables cannot hold `nil`. To represent the absence of a value, Swift uses Optionals, which are technically an enumeration (`Optional<Wrapped>`) that wraps the underlying type. An optional type is denoted by appending a question mark (`?`) to the type signature.

```swift theme={"dark"}
// Standard typed variable (cannot be nil)
var standardInt: Int = 5 

// Optional typed variable (can hold an Int or nil)
var optionalInt: Int? = nil 
optionalInt = 10
```

**Collection Typing**
Variables holding collections (like Arrays, Dictionaries, or Sets) are strongly typed using generics to define the specific type of elements they are permitted to store.

```swift theme={"dark"}
// Array strictly typed to hold Strings
var stringArray: [String] = ["Alpha", "Beta"]

// Dictionary strictly typed with String keys and Int values
var mappedValues: [String: Int] = ["Key": 100]
```

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