Swift Data Types
A data type tells Swift what kind of value a variable holds. Knowing the type lets Swift catch mistakes early — like trying to add a word to a number — before your app crashes on a user's phone.
Core Data Types
Int — Whole Numbers
An Int stores whole numbers with no decimal point.
var apples: Int = 10
var temperature: Int = -5
var population: Int = 1_000_000The underscore in 1_000_000 is just a visual separator. Swift ignores it, so the value is exactly one million.
Double — Decimal Numbers
A Double stores numbers with a decimal point. Use it for measurements, prices, and percentages.
var price: Double = 9.99
var gpa: Double = 3.75
var pi: Double = 3.14159Float — Smaller Decimal Numbers
Float also holds decimals but uses less memory and has lower precision than Double. Swift prefers Double for most tasks.
var weight: Float = 72.5String — Text
A String holds a sequence of characters. Always wrap string values in double quotes.
var greeting: String = "Hello, World!"
var country: String = "Japan"Character — Single Letter
A Character holds exactly one letter, digit, or symbol.
var grade: Character = "A"
var symbol: Character = "@"Bool — True or False
A Bool holds only two possible values: true or false. Use it to represent yes/no or on/off states.
var isLoggedIn: Bool = true
var hasSubscription: Bool = falseDiagram: Data Types at a Glance
Type | Example Value | Use Case ------------+-------------------+--------------------- Int | 42, -7, 1000 | Counts, ages, scores Double | 3.14, -0.5, 9.99 | Prices, measurements Float | 1.5, 72.3 | Low-precision decimals String | "Swift", "Hello" | Names, messages, text Character | "A", "9", "@" | Single letters/symbols Bool | true, false | Flags, conditions
Type Inference in Action
Swift reads the value you assign and picks the right type automatically.
let city = "Paris" // String
let floors = 30 // Int
let height = 324.0 // Double
let isOpen = true // BoolType Conversion
Swift does not mix types automatically. You must convert explicitly.
Int to Double
let apples: Int = 5
let price: Double = 1.5
let total = Double(apples) * price
print(total) // Output: 7.5Double to Int
let score: Double = 98.7
let roundedScore: Int = Int(score)
print(roundedScore) // Output: 98 (decimal is dropped)Int to String
let count: Int = 5
let message: String = "You have \(count) messages."
print(message) // Output: You have 5 messages.String Operations
Concatenation
let first = "Swift"
let second = " is fun"
let result = first + second
print(result) // Output: Swift is funString Length
let word = "Hello"
print(word.count) // Output: 5Multiline Strings
let poem = """
Roses are red,
Violets are blue,
Swift is fast,
And so are you.
"""
print(poem)Checking Types with type(of:)
You can ask Swift what type a value is at runtime using type(of:).
let value = 42
print(type(of: value)) // Output: Int
let name = "Alice"
print(type(of: name)) // Output: StringConstants and Computed Values
You can create new values from existing ones without changing the originals.
let length: Double = 5.0
let width: Double = 3.0
let area = length * width
print("Area: \(area)") // Output: Area: 15.0Summary
Swift's main data types are Int, Double, String, Character, and Bool. Swift infers types automatically, but it never converts between them silently. Always use explicit conversion like Int() or Double() when mixing types in calculations.
