Introduction
Swift is a type-safe language, meaning every value has a known type at compile time and the compiler checks that types match wherever they are used. The fundamental data types include Int for whole numbers, Double and Float for floating-point numbers, Bool for true/false values, String for text, and Character for a single grapheme cluster. Swift also provides tuples, a lightweight way to group multiple values of possibly different types into a single compound value.
Cricket analogy: Swift's type safety is like a scorer who won't let you add a boundary count (Int) directly to a batting average (Double) without an explicit conversion; player names are Strings, a single jersey-number digit is a Character, whether a review upheld is a Bool, and a tuple like (name: String, runs: Int) bundles a batsman's name and score together without needing a full struct.
Syntax
let age: Int = 30
let price: Double = 19.99
let ratio: Float = 0.5
let isActive: Bool = true
let name: String = "Ada"
let initial: Character = "A"
let point: (Int, String) = (1, "first")Explanation
Int represents whole numbers and, on modern platforms, is 64-bit by default. Double offers 64-bit floating-point precision and is Swift's preferred type for decimal numbers, while Float offers 32-bit precision. Bool holds only true or false. String represents text as a collection of Character values, and Character represents a single extended grapheme cluster. Unlike some languages, Swift does not perform implicit conversion between numeric types: an Int and a Double cannot be combined in an expression without an explicit conversion, which prevents subtle precision-loss bugs. Tuples, written as a comma-separated list in parentheses like (Int, String), let you bundle related values together without defining a full struct.
Cricket analogy: A run total is naturally an Int, 64-bit by default on modern platforms, while a batting average uses Double's 64-bit precision rather than Float's 32-bit for accuracy; a wicket-fallen flag is a Bool, a player's name is a String made of Character grapheme clusters, and Swift won't silently combine that Int run count with the Double average — you must convert explicitly, just as a tuple (name: String, average: Double) bundles the two without a full struct.
Example
let itemCount = 3 // Int
let unitPrice = 2.5 // Double
// let total = itemCount * unitPrice // Error: Int and Double cannot be combined
let total = Double(itemCount) * unitPrice
let coordinates: (Int, Int) = (10, 20)
print(coordinates.0, coordinates.1)
print(total)Output
10 20
7.5Key Takeaways
- Swift's core types include Int, Double, Float, Bool, String, and Character.
- Swift does not implicitly convert between numeric types like Int and Double.
- Explicit conversion, such as Double(someInt), is required to mix numeric types.
- Double is generally preferred over Float for its greater precision.
- Tuples group multiple values of possibly different types, e.g. (Int, String).
- Every value in Swift has a well-defined type checked at compile time.
Practice what you learned
1. Which type would you use to store a decimal price value with high precision in Swift?
2. What happens if you try to add an Int and a Double directly in Swift, e.g. 3 + 2.5?
3. What is a tuple in Swift?
4. Which type represents a single extended grapheme cluster in Swift?
Was this page helpful?
You May Also Like
Variables and Constants in Swift
Learn how Swift's var and let keywords declare mutable variables and immutable constants, and why let is preferred by default.
Type Inference and Conversion in Swift
Learn how Swift infers types from literal values and why converting between types always requires an explicit initializer call.
Optionals in Swift
Optionals let a Swift variable represent either a value or the absence of one, forming the basis of Swift's null-safety.
Arrays in Swift
Learn how Swift arrays store ordered, mutable collections of values and how to manipulate them safely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics