Swift Data Types
Swift's basic building blocks — Int, Double, String, and Bool — combine with type inference to give you both safety and brevity in the same line of code.
The Core Data Types
Every value in Swift has a type, and the four you will reach for constantly are Int for whole numbers, Double for decimal numbers, String for text, and Bool for true/false values. Swift is a strongly typed language, meaning a value's type is fixed once it's created and cannot silently change into something else.
The four core types
let age: Int = 29
let price: Double = 19.99
let name: String = "Ada"
let isMember: Bool = true
print(age, price, name, isMember)Type Inference
You rarely need to write type annotations in Swift because the compiler infers the type from the value on the right-hand side of the assignment. Writing let age = 29 is exactly equivalent to writing let age: Int = 29 — the compiler chooses Int because 29 has no decimal point. This keeps code concise while remaining fully type-safe.
Letting Swift infer types
let inferredInt = 42 // Int
let inferredDouble = 3.14 // Double
let inferredString = "hello" // String
let inferredBool = false // Bool
// Mixing Int and Double directly is not allowed:
// let total = inferredInt + inferredDouble // errorInt and Double in Practice
Int represents whole numbers and is the default choice for counting, indexing, and any value that shouldn't have a fractional part. Double represents 64-bit floating-point numbers and is the default choice for decimal math — even things like 2.0 are Double unless you say otherwise. Swift also has Float for lower-precision decimals, but Double is preferred unless memory is extremely tight.
Working with numbers
var itemCount = 5 // Int
let unitPrice = 2.5 // Double
let total = Double(itemCount) * unitPrice
print(total) // 12.5- Int — whole numbers, positive or negative, no decimal point.
- Double — decimal numbers with high precision, the default floating-point type.
- String — text, always enclosed in double quotes.
- Bool — exactly two values: true or false, used for conditions and flags.
Type Overview
Exercise: Swift Data Types
What type does Swift infer for let x = 10?