vaayaa Inclusive Education
Micro-Diploma

iOS App Development

5-Year Access Self-Paced Learning Certificate Included

Unlock Full Course

Get instant access to all sections and earn your verified certificate.

$9.99

Table of Contents

FREE PREVIEW — Section 1 of Module 1

Variables, Constants, and Swift's Type System

Unit

Swift is a statically typed language, which means every variable and constant has a fixed type determined at compile time. This design choice catches a large class of bugs before your app ever runs. You declare variables with the var keyword and constants with let. Preferring let wherever a value does not need to change is a core Swift convention because it communicates intent clearly and enables compiler optimizations.

Swift's type inference engine can deduce the type of a value from its initial assignment, so you rarely need to write explicit type annotations in straightforward cases. When you write let score = 42, the compiler infers score as Int. Explicit annotations become useful when you need a specific numeric type, such as Double instead of Int, or when declaring a variable without an initial value.

The language provides a rich set of built-in types: Int for whole numbers, Double and Float for floating-point values, Bool for true/false logic, and String for text. Swift strings are Unicode-correct by default, meaning they handle international characters, emoji, and complex scripts without extra configuration. String interpolation with the \() syntax lets you embed expressions directly inside string literals, keeping formatting code concise.

Optionals are one of Swift's most distinctive features. An optional type, written as Type?, represents a value that may or may not exist. Rather than using null references that crash at runtime, Swift forces you to explicitly handle the absence of a value through optional binding with if let or guard let, the nil-coalescing operator ??, or optional chaining with ?.. This system eliminates an entire category of null-pointer crashes that plague other languages.

Code Example
Swift
// Constants and variables
let appName: String = 'Northbridge Tracker'
var currentScore = 0
var playerName: String? = nil

// Optional binding
if let name = playerName {
print('Welcome, \(name)')
} else {
print('Welcome, Guest')
}

// Nil-coalescing operator
let displayName = playerName ?? 'Anonymous'

// String interpolation
currentScore += 10
print('Score is now \(currentScore)')

This example shows let for an immutable app name, var for a mutable score, and an optional String? for a player name that may not yet exist. The if let binding safely unwraps the optional, and the ?? operator provides a fallback value without crashing.

Quick Revision
  • Use let for constants and var for variables; prefer let by default.
  • Swift infers types automatically from initial values, reducing boilerplate code.
  • Built-in types include Int, Double, Bool, and String.
  • Optionals (Type?) represent values that may be absent, preventing null-pointer crashes.
  • Use if let, guard let, or ?? to safely work with optional values.

Like what you see? Unlock the full course to continue.

Limited Offer
$9.99
Limited Offer $9.99