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 // error
Note: Swift never automatically converts between numeric types. Adding an Int to a Double requires an explicit conversion, which you'll see in the Type Casting lesson.

Int 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

TypeExample literalInferred when you write
Int42A number with no decimal point
Double3.14A number containing a decimal point
String"Swift"Text wrapped in double quotes
Booltrue / falseOne of the two literal keywords
Note: String literals must use double quotes, not single quotes — Swift has no single-quote string syntax, so 'hello' is invalid.

Exercise: Swift Data Types

What type does Swift infer for let x = 10?