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, …)

Swift

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)

Swift

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!)

Swift

Why not Double for money?

Swift

Rule: Double → measurements, coordinates, animations Decimal → money, prices, financial calculations

3.4 Boolean (Bool)

Swift

Very often used in conditions:

Swift

3.5 Text (String – very important!)

Swift

Useful things you do every day:

Swift

3.6 Single character (Character) – rare

Swift

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

Swift

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 😊

You may also like...

Leave a Reply

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