Swift Data Types: Strings, Integers, Doubles, and Booleans


When Woody introduces himself, he says his name — that’s text. Buzz Lightyear’s top speed is a number. Whether or not Rex is scary is a true-or-false question. Every piece of information in your app has a type, and Swift needs to know what type each value is before it will let you use it.

In this guide, you’ll learn Swift’s fundamental data types — String, Int, Double, Float, and Bool — and how Swift’s type safety keeps your code free from an entire category of bugs. We won’t cover collections or custom types here — those come in later posts.

This guide assumes you’re familiar with variables and constants.

What You’ll Learn

What Are Data Types?

A data type tells Swift what kind of value a variable or constant holds. Think of it like the label on a shelf in the Monsters, Inc. mailroom. One shelf is labeled “Scream Canisters” (numbers), another is labeled “Door Cards” (text), and a third is labeled “Active/Inactive” (true or false). You can’t put a scream canister on the Door Cards shelf — they don’t belong together.

Swift is a type-safe language. This means the compiler checks that you never accidentally mix up types, catching mistakes before your code runs.

Strings — Text

A String holds text — any combination of letters, numbers, symbols, and emoji surrounded by double quotes:

let characterName = "Woody"
let catchphrase = "There's a snake in my boot!"
let emoji = "🤠"
print(characterName)
print(catchphrase)
Woody
There's a snake in my boot!

You can combine strings using the + operator:

let firstName = "Buzz"
let lastName = "Lightyear"
let fullName = firstName + " " + lastName
print(fullName)
Buzz Lightyear

An empty string is created with "" — it’s still a valid String, just with no characters inside.

Apple Docs: String — Swift Standard Library

Integers — Whole Numbers

An Int holds whole numbers — no decimal points. Use integers for things you count: number of movies, year of release, or a character’s age.

let releaseYear = 1995
let sequelCount = 4
let negativeNumber = -10
print("Toy Story (\(releaseYear)) has \(sequelCount) films")
Toy Story (1995) has 4 films

You can do math with integers:

let first = 10
let second = 3
print(first + second)
print(first - second)
print(first * second)
print(first / second)
13
7
30
3

Note: Integer division drops the decimal — 10 / 3 gives 3, not 3.333. If you need decimals, use Double.

Doubles and Floats — Decimal Numbers

A Double holds numbers with decimal points, like ratings or prices. It’s short for “double-precision floating-point number” and provides about 15 digits of precision.

let rating = 8.3
let piValue = 3.14159265358979
print("Rating: \(rating)")
print("Pi: \(piValue)")
Rating: 8.3
Pi: 3.14159265358979

Swift also has Float, which uses less memory but is less precise (about 6 digits). In practice, always use Double unless you have a specific reason to use Float.

let floatValue: Float = 8.3
let doubleValue: Double = 8.3
print("Float:  \(floatValue)")
print("Double: \(doubleValue)")
Float:  8.3
Double: 8.3

Tip: When you write a decimal number like 8.3 without a type annotation, Swift automatically makes it a Double. This is Swift’s type inference at work.

Booleans — True or False

A Bool holds exactly one of two values: true or false. Booleans answer yes-or-no questions in your code.

let isAnimated = true
let hasSequel = false
print("Animated: \(isAnimated)")
print("Has sequel: \(hasSequel)")
Animated: true
Has sequel: false

Booleans are essential for making decisions. You’ll use them constantly with control flow statements like if and while.

You can flip a boolean with the ! operator (logical NOT) or the .toggle() method:

var lightsOn = true
lightsOn.toggle()
print(lightsOn)
false

Type Safety

Swift’s type safety means you can’t accidentally mix types. If a variable is a String, you can’t put a number in it:

var movieTitle = "Up"
// movieTitle = 42  // ❌ Error: cannot assign Int to String

This might feel restrictive at first, but it prevents a huge class of bugs. Imagine if your app displayed a movie’s rating where its title should be — type safety catches that mistake at compile time, before any user ever sees it.

Note: Type safety is checked at compile time — meaning Swift catches these errors while you’re writing code, not after your app is already running.

Type Conversion

Sometimes you need to convert between types. Swift requires you to be explicit about this — no silent, automatic conversions:

let year = 1995
let yearText = String(year)
print("Released in " + yearText)
Released in 1995

Converting between number types works the same way:

let wholeNumber = 42
let decimal = Double(wholeNumber)
print(decimal)
42.0

Going the other direction, Int() truncates the decimal — it doesn’t round:

let rating = 8.7
let rounded = Int(rating)
print(rounded)
8

Warning: Int(8.7) gives 8, not 9. It always truncates toward zero. If you need rounding, use Int(rating.rounded()) instead.

Common Mistakes

Mixing Types Without Converting

// ❌ Won't compile
let year = 1995
let message = "Released in " + year
// ✅ Convert explicitly
let year = 1995
let message = "Released in \(year)"
print(message)
Released in 1995

String interpolation (\()) handles the conversion for you and is the preferred approach in Swift.

Using Float When You Mean Double

// ❌ Unnecessary precision loss
let rating: Float = 8.33333333
print(rating)
8.333333
// ✅ Use Double for better precision
let rating: Double = 8.33333333
print(rating)
8.33333333

Unless you’re working with APIs that specifically require Float, stick with Double.

What’s Next?

  • String stores text, Int stores whole numbers
  • Double stores decimal numbers with high precision
  • Bool stores true or false
  • Swift is type-safe — it won’t let you mix types
  • Use explicit conversion like String(42) or Double(10) when you need to change types

You may have noticed that we didn’t always write out the type when declaring variables. That’s because Swift can often figure it out on its own. Head over to Type Inference to learn how the compiler reads your mind.