Chapter 17: Data Types
1. What is a data type? (very simple explanation)
A data type tells the computer:
- What kind of value this is (number, text, true/false, list, …)
- How much memory it needs
- What operations are allowed on it (+, ==, .count, …)
Swift is a strongly typed language — it checks types very carefully (much stricter than Python or JavaScript).
2. Two big families of types in Swift
| Family | Examples | When you copy it, what happens? | Main feeling / usage |
|---|---|---|---|
| Value types | Int, Double, Bool, String, Array, Dictionary, Set, struct, enum | A copy is made (independent) | Most everyday types — safe & predictable |
| Reference types | class, objects that inherit from NSObject, some closures | Only a reference is copied (shared) | Used for shared state (like view controllers) |
Beginner rule (very important):
In 95% of beginner and intermediate code you will mostly meet value types You will see class later (UIKit, SwiftUI view models, networking models sometimes)
3. The most important built-in data types (with examples)
Let’s go through them one by one — with code you can run right now.
3.1 Integer types (Int, UInt, Int64, …)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
let age = 25 // Int (most common) let likes = 1_245_678 // underscore makes big numbers readable let negativeBalance = -150 let veryBigNumber = 9_223_372_036_854_775_807 // Int.max on 64-bit let smallCounter: UInt8 = 255 // only 0...255 let fileSizeBytes: Int64 = 8_589_934_592 // when you need exactly 64-bit |
Quick table – integer types you might meet
| Type | Range (approx) | Signed? | Typical use case |
|---|---|---|---|
| Int | -9 quintillion … +9 quintillion | Yes | Normal counting, IDs, indices (default) |
| UInt | 0 … 18 quintillion | No | Rarely — when you really want non-negative |
| Int64 | -9 quintillion … +9 quintillion | Yes | Large file sizes, timestamps |
| UInt8 | 0 … 255 | No | Colors (RGB), small counters |
3.2 Floating-point types (Double vs Float)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let height = 1.72 // Double – most common choice let temperature = 36.6 let pi = 3.141592653589793 let verySmallNumber = 1.23e-6 // scientific notation let opacity: Float = 0.75 // 32-bit float – less precise |
Very important rule most beginners miss:
Use Double for almost everything Use Float only when:
- You need to save memory (very rare)
- You are working with old APIs / graphics that expect Float
3.3 Money – special case: Decimal (not Double!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import Foundation let itemPrice: Decimal = 1499.99 let quantity: Int = 3 let taxRate: Decimal = 0.18 let subtotal = itemPrice * Decimal(quantity) let tax = subtotal * taxRate let total = subtotal + tax print(total) // 5311.9702 |
Why not Double for money?
|
0 1 2 3 4 5 6 7 |
let bad = 0.1 + 0.2 // 0.30000000000000004 ← floating point error! let good: Decimal = 0.1 + 0.2 // exactly 0.3 |
Rule: Double → measurements, coordinates, animations Decimal → money, prices, financial calculations
3.4 Boolean (Bool)
|
0 1 2 3 4 5 6 7 8 9 |
let isLoggedIn = true let hasPremium = false let isRaining = false let canSubmitForm = emailIsValid && passwordMeetsRequirements |
Very often used in conditions:
|
0 1 2 3 4 5 6 7 8 9 10 |
if isLoggedIn { showDashboard() } else { showLoginScreen() } |
3.5 Text (String – very important!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let name = "Rahul Verma" let greeting = "నమస్తే Hyderabad! 🌶️" let multiline = """ First line Second line Third line with emoji 😺 """ let empty = "" let singleCharString = "A" // still String, not Character |
Useful things you do every day:
|
0 1 2 3 4 5 6 7 8 9 10 |
print(name.count) // 11 print(name.uppercased()) // RAHUL VERMA print(name.hasPrefix("Rah")) // true print(name.contains("ul")) // true print(greeting.contains("🌶️")) // true |
3.6 Single character (Character) – rare
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let grade: Character = "A" let currency: Character = "₹" let smile: Character = "😊" // This is NOT a Character — it's a String! let wrong = "A" // String with one character |
Rule of thumb: You almost never need Character — use String even for single letters/symbols.
4. Quick real-life examples – how types appear in apps
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
struct Product { let id: String // or UUID let name: String let price: Decimal // money! let rating: Double // 4.78 let reviewCount: Int let inStock: Bool let tags: [String] // we'll talk about Array later } let iphone = Product( id: "MNH63HN/A", name: "iPhone 16 Pro", price: 119900, rating: 4.85, reviewCount: 12450, inStock: true, tags: ["smartphone", "apple", "5G"] ) |
5. Very common beginner mistakes
| Mistake | Wrong code | Correct approach | Why? |
|---|---|---|---|
| Using Float for normal decimals | let price: Float = 99.99 | let price = 99.99 // Double | Double is much more precise |
| Using Double for money | let total = 123.45 + 0.01 | let total: Decimal = 123.45 + 0.01 | Floating-point rounding errors |
| Forgetting String is value type | Expecting shared mutation | var a = b; a += “!” → b unchanged | Value types are copied |
| Using Int32 / UInt32 everywhere | let count: Int32 = 100 | let count = 100 // Int | Int is the normal choice |
| Declaring single letters as Character | let letter: Character = “A” everywhere | let letter = “A” | String is more flexible |
6. Summary table – most common types in real apps
| Purpose | Recommended type | Example value |
|---|---|---|
| Counting, indices, IDs | Int | userCount = 1420 |
| Money, prices, financial | Decimal | price = 2499.99 |
| Measurements, coordinates, science | Double | temperature = 36.6 |
| True / false flags | Bool | isPremium = true |
| Text, names, messages, JSON | String | username = “sneha_dev” |
| Single symbol (rare) | Character | currency = “₹” |
| Very large numbers | Int64 / UInt64 | fileSizeBytes = 8_589_934_592 |
| Small counters / bytes | UInt8 | red: UInt8 = 255 |
Which data type or group would you like to explore much deeper next?
- String in much more detail (very important)
- Decimal vs Double – with real money examples
- Collections (Array, Dictionary, Set)
- Optionals (Int?, String?, nil)
- Or start with structs & enums
Just tell me — we’ll continue in the same slow, clear, teacher-like style 😊
