Swift Data Types

Every value in Swift belongs to a type. A type tells Swift what kind of data you are storing — a number, text, or true/false value. Swift uses types to prevent you from mixing incompatible data accidentally.

The Grocery Bag Analogy


┌─────────────────────────────────────────────────┐
│        Data Types = Different Compartments      │
│                                                 │
│  Int bag     →  holds whole numbers: 1, 42, -7  │
│  Double bag  →  holds decimals: 3.14, 99.9      │
│  String bag  →  holds text: "Hello", "Swift"    │
│  Bool bag    →  holds true or false only        │
│                                                 │
│  You cannot put an apple in the orange bag.     │
│  Swift enforces this rule automatically.        │
└─────────────────────────────────────────────────┘

Int – Whole Numbers

var age: Int = 25
var floors: Int = 12

Int stores whole numbers — positive, negative, or zero. It cannot hold decimals like 3.14.

Double – Decimal Numbers

var price: Double = 49.99
var pi: Double = 3.14159

Double stores numbers with a decimal point. Use it for prices, measurements, and scientific values. Swift also has Float, which is less precise than Double.

String – Text

var greeting: String = "Good morning"
var country: String = "India"

String stores any text — names, messages, and labels. Always wrap String values in double quotes.

Bool – True or False

var isLoggedIn: Bool = true
var hasPaid: Bool = false

Bool holds only two values: true or false. You use it for decisions like "Is the user signed in?"

Character – A Single Letter

var grade: Character = "A"

Character stores exactly one letter or symbol. A String is a sequence of Characters.

Type Inference – Swift Guesses the Type

var score = 100      // Swift infers: Int
var rate = 4.5       // Swift infers: Double
var name = "Priya"   // Swift infers: String
var isActive = true  // Swift infers: Bool

You do not always need to write the type. Swift reads the value and figures it out. This feature is called type inference.

Type Annotation – You Specify the Type

var distance: Double = 10   // forces Double, not Int

When you need precise control, you write the type after a colon. Here, distance becomes a Double even though 10 looks like an integer.

Type Safety – No Mixing Allowed

var quantity: Int = 5
// quantity = "five"   // Error! Cannot assign String to Int

Swift blocks this at compile time. Once a variable has a type, only values of that same type can go into it.

Type Conversion

var total: Int = 10
var discount: Double = 2.5
var result = Double(total) - discount  // 7.5

To combine different types, convert one explicitly. Double(total) turns the integer 10 into 10.0, making the subtraction work correctly.

Quick Reference Table

TypeStoresExample Value
IntWhole numbers42
DoubleDecimal numbers3.14
StringText"Swift"
BoolTrue or falsetrue
CharacterSingle character"A"

Leave a Comment

Your email address will not be published. Required fields are marked *