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 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).
// 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!).
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.
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.
// 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.
// 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]
Master Swift with Deep Grasping Methodology!Learn More