Swift Data Types Explained: Int, String, Double, Bool, and More

Swift data types define what kind of value a variable or constant can store. If you want to write correct Swift code, you need to understand the basic built-in types such as Int, String, Double, Bool, Float, and Character. In this guide, you will learn what each type does, how to use them in real code, how Swift infers types automatically, and how to avoid common mistakes like type mismatch errors.

Quick answer: Swift data types describe the kind of data a value represents, such as whole numbers, text, decimal numbers, or true/false values. The most common basic types are Int for whole numbers, String for text, Double for decimal numbers, and Bool for true or false values.

Difficulty: Beginner

Helpful to know first: You'll understand this better if you know basic Swift syntax, how to declare variables and constants, and how simple values are printed with print().

1. What Is Swift Data Types?

A data type tells Swift what kind of value is being stored or passed around in code. This matters because Swift is a type-safe language, which means it checks that you use values in sensible ways.

For example, a name should be stored as text, an age is usually a whole number, and a price may need a decimal number. Swift uses types to make these differences clear.

Beginners often compare Double and Float, or Character and String. Those comparisons matter because the values may look similar, but Swift treats them as different types with different purposes.

2. Why Swift Data Types Matters

Data types matter because they help Swift catch mistakes early, make your code easier to read, and ensure operations behave correctly.

If Swift did not care about types, you could accidentally add text to a number, compare unrelated values incorrectly, or lose numeric precision without noticing. By enforcing types, Swift helps prevent many bugs at compile time.

In real programs, data types are used everywhere:

Swift's type system is one of the reasons Swift code is usually safer and easier to maintain than loosely typed code.

3. Basic Syntax or Core Idea

In Swift, you can declare a variable or constant with an explicit type, or let Swift infer the type from the assigned value.

Declaring values with explicit types

This example shows the main built-in data types written clearly with type annotations.

let age: Int = 25
let name: String = "Maya"
let temperature: Double = 21.5
let isOpen: Bool = true
let grade: Character = "A"

Each line tells Swift exactly what kind of value is being stored. This is useful when you want your code to be extra clear.

Using type inference

Swift can often determine the type automatically based on the value you assign.

let count = 10
let city = "London"
let price = 19.99
let isReady = false

Here, Swift infers that count is an Int, city is a String, price is a Double, and isReady is a Bool.

Reading the core idea

The main rule is simple: the value and the type must match. If they do not match, Swift usually gives a compile-time error instead of letting incorrect code run.

4. Step-by-Step Examples

Example 1: Using Int for whole numbers

Use Int when you need numbers without decimal places, such as a score or quantity.

let apples = 4
let oranges = 6
let totalFruit = apples + oranges

print(totalFruit)

This prints 10. Because all values are whole numbers, Int is the natural choice.

Example 2: Using String for text

Use String when you need words, sentences, or other text data.

let firstName = "Sam"
let lastName = "Lee"
let fullName = firstName + " " + lastName

print(fullName)

This prints Sam Lee. Swift uses + to join strings together.

Example 3: Using Double for decimal values

Use Double for decimal values such as prices, distances, or averages.

let itemPrice = 12.5
let tax = 1.25
let totalPrice = itemPrice + tax

print(totalPrice)

This prints 13.75. Because the numbers include decimal places, Swift infers Double.

Example 4: Using Bool for conditions

Use Bool when something can be true or false.

let isLoggedIn = true

if isLoggedIn {
    print("Welcome back!")
} else {
    print("Please sign in.")
}

A boolean value is commonly used in conditions because if statements expect a Bool.

Example 5: Character vs String

A single letter may look like a short string, but Swift distinguishes Character from String.

let letter: Character = "Z"
let word: String = "Zebra"

print(letter)
print(word)

letter stores one character, while word stores a sequence of characters. Even though both use double quotes in Swift, they are still different types.

5. Practical Use Cases

6. Common Mistakes

Mistake 1: Mixing Int and Double without conversion

Swift does not automatically mix numeric types in arithmetic expressions. Even though Int and Double are both numbers, they are not interchangeable.

Problem: This code tries to add an Int and a Double directly, which causes a type mismatch error such as Binary operator '+' cannot be applied to operands of type 'Int' and 'Double'.

let items = 3
let pricePerItem = 4.99
let total = items + pricePerItem

Fix: Convert one value so both sides use the same numeric type.

let items = 3
let pricePerItem = 4.99
let total = Double(items) * pricePerItem

The corrected version works because both values are treated as Double before the calculation.

Mistake 2: Treating a String like a number

Text that looks like a number is still text if it is stored in a String. Swift will not automatically treat it as numeric data.

Problem: This code tries to add a string and an integer, which leads to a compile-time error such as Binary operator '+' cannot be applied to operands of type 'String' and 'Int'.

let quantityText = "5"
let extraItems = 2
let totalItems = quantityText + extraItems

Fix: Convert the string to an integer first if it truly represents a number.

let quantityText = "5"
let extraItems = 2

if let quantity = Int(quantityText) {
    let totalItems = quantity + extraItems
    print(totalItems)
}

The corrected version works because the text is safely converted into an Int before arithmetic is performed.

Mistake 3: Using text instead of Bool in conditions

In some languages, developers write values like "true" as text and expect them to behave like booleans. Swift does not allow that.

Problem: An if statement requires a Bool, not a String, so this code fails with an error such as Cannot convert value of type 'String' to expected condition type 'Bool'.

let isMember = "true"

if isMember {
    print("Discount applied")
}

Fix: Store true/false values using the Bool type.

let isMember = true

if isMember {
    print("Discount applied")
}

The corrected version works because the condition now uses an actual boolean value.

Mistake 4: Confusing Character with String

A single letter and a piece of text may look similar, but Swift still treats them as separate types.

Problem: This code assigns a string value to a variable declared as Character, but the value contains more than one character.

let initial: Character = "AB"

Fix: Use a single-character value for Character, or change the type to String.

let initial: Character = "A"
let initials: String = "AB"

The corrected version works because each value now matches the declared type exactly.

7. Best Practices

Use the most accurate type for the job

Choosing the right type makes your code easier to understand and helps prevent mistakes. Do not use text for values that should be numeric or boolean.

// Less preferred
let ageText = "30"

// Preferred
let age = 30

The preferred version is better because the data is stored in a form that matches how it will be used.

Prefer type inference when the type is obvious

Swift's type inference keeps code shorter and cleaner. Use explicit types when they improve clarity, not by default everywhere.

// Less preferred when the type is already obvious
let username: String = "coder123"

// Preferred
let username = "coder123"

This keeps the code readable while still letting Swift know exactly what type to use.

Use Double as the default decimal type

In standard Swift code, Double is usually the better default for decimal numbers because it offers more precision than Float.

// Less preferred unless an API specifically needs Float
let distance: Float = 5.75

// Preferred
let distance: Double = 5.75

This is a good default choice for most everyday calculations in Swift.

Choose clear boolean names

Boolean values are easier to read when their names sound like yes/no questions or states.

// Less preferred
let status = true

// Preferred
let isAvailable = true

The second name makes conditions much easier to read later in the code.

8. Limitations and Edge Cases

A common "not working" situation is integer division. For example, 5 / 2 gives 2 with Int values, not 2.5. Convert to Double if you need a fractional result.

9. Practical Mini Project

This small program uses several Swift data types together to represent a simple product summary. It shows how whole numbers, decimals, text, and booleans work together in one realistic example.

let productName: String = "Wireless Mouse"
let quantity: Int = 2
let pricePerItem: Double = 24.99
let isInStock: Bool = true
let categoryCode: Character = "E"

let totalCost = Double(quantity) * pricePerItem

print("Product:", productName)
print("Quantity:", quantity)
print("Price per item:", pricePerItem)
print("In stock:", isInStock)
print("Category code:", categoryCode)
print("Total cost:", totalCost)

This example is useful because each value uses a type that matches its meaning. The final line also shows explicit type conversion from Int to Double before multiplication.

10. Key Points

11. Practice Exercise

Build a small profile summary using multiple Swift data types.

Expected output: A short summary showing the name, age, balance, and active status.

Hint: Choose the type that matches the meaning of each value, and use print() to display the result.

let name: String = "Ava"
let age: Int = 28
let balance: Double = 152.75
let isActive: Bool = true

print("Name:", name)
print("Age:", age)
print("Balance:", balance)
print("Active:", isActive)

12. Final Summary

Swift data types are one of the first core ideas every Swift developer should understand. They tell Swift what kind of data a value contains and allow the compiler to catch mistakes early. The most common types you will use are Int for whole numbers, String for text, Double for decimal values, and Bool for true/false conditions.

You also saw that Swift supports type inference, which lets the compiler determine types automatically in many situations. At the same time, Swift remains strict about type safety, so you often need explicit conversions when working with different kinds of values. That strictness may feel limiting at first, but it helps you write safer and more predictable code.

A good next step is to learn about Swift variables and constants, operators, and type conversion in more detail. Those topics build directly on what you learned here and will help you use Swift data types more confidently in real programs.