Chapter 1: Swift Introduction
What is Swift? (Why Learn It in 2025?)
Swift is Apple’s modern programming language, first released in 2014. It replaced the older Objective-C for building apps on iPhone, iPad, Mac, Apple Watch, Apple TV, and even visionOS (AR/VR).
Why Swift is Great in 2025–2026:
- Safe — Prevents many common bugs (especially with optionals and concurrency)
- Fast — Close to C++ speed, but easier to write
- Modern — Clean syntax, powerful features like async/await, actors, macros
- Multi-platform — Apple platforms + server-side (Vapor), Linux, Windows, even Android (experimental)
- Swift 6.2 (latest as of late 2025) focuses on:
- Easier concurrency (no more data races by default)
- Better performance tools
- Improved debugging & build times
- Great interoperability with C++, Java, etc.
In short: If you want to build beautiful iOS/macOS apps, or even backend APIs, Swift is one of the best choices today.
Chapter 2: How to Start Writing Swift Code Right Now (No Mac Needed)
Option 1 – Best for beginners Download Swift Playgrounds (free on Mac or iPad) → Open → tap + → Blank playground
Option 2 – No Mac? Go to https://swiftfiddle.com → paste code → run
Option 3 – Serious learning Install Xcode (free from App Store on Mac) later.
Let’s write your first line:
|
0 1 2 3 4 5 6 |
print("Namaste, Hyderabad! 🌶️") |
Run it → you’ll see:
|
0 1 2 3 4 5 6 |
Namaste, Hyderabad! 🌶️ |
Congratulations — you just ran Swift code!
Chapter 3: Variables & Constants (let vs var)
Think of variables as boxes with labels.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
// Constant – cannot change (use this most of the time!) let city = "Hyderabad" // Variable – can change var temperature = 28 temperature = 30 temperature += 2 // now 32 |
Golden rule (very important!):
- Use letby default
- Use varonly when you know the value will change
Common beginner mistake:
|
0 1 2 3 4 5 6 |
var name = "Rahul" // ← wrong if you never change it! |
Better:
|
0 1 2 3 4 5 6 |
let name = "Rahul" |
Chapter 4: Basic Data Types
| Type | Examples | Real-life use case |
|---|---|---|
| Int | 42, -5, 1000000 | Age, count, ID |
| Double | 3.14, 1.76, -0.001 | Height, price, temperature |
| Bool | true, false | Is raining? Is logged in? |
| String | “Hello”, “আমি বাংলা ভালোবাসি” | Names, messages, text |
Examples:
|
0 1 2 3 4 5 6 7 8 9 |
let age: Int = 28 let height: Double = 1.68 let lovesSwift: Bool = true let greeting = "Hello from Telangana! 🚩" |
Swift usually guesses the type (type inference) — very nice!
Chapter 5: Strings – The Most Used Type
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
let firstName = "Ananya" let lastName = "Banerjee" // Modern way (interpolation) let fullName = "\(firstName) \(lastName)" // "Ananya Banerjee" // Old way (concatenation) let fullOld = firstName + " " + lastName // Multi-line let bio = """ I love Tagore's poems, Swift programming, and hot chai on rainy days ☕ """ |
Useful string methods:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let text = "Swift is beautiful!" print(text.count) // 20 print(text.uppercased()) // SWIFT IS BEAUTIFUL! print(text.hasPrefix("Swift")) // true print(text.contains("chai")) // false |
Chapter 6: Simple Decisions – if & switch
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let temp = 32 if temp > 35 { print("Too hot! 🥵") } else if temp > 25 { print("Perfect Hyderabad summer 🌞") } else { print("Chai time! ☕") } |
Switch (very clean in Swift):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let mood = "happy" switch mood { case "happy", "excited": print("Great! 🎉") case "tired": print("Take a break") default: print("How are you feeling?") } |
Chapter 7: Arrays – Lists of Things
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create var fruits = ["Mango", "Banana", "Guava"] // Add fruits.append("Apple") // Access print(fruits[0]) // "Mango" // Loop for fruit in fruits { print("I love \(fruit)!") } |
With index:
|
0 1 2 3 4 5 6 7 8 |
for (index, fruit) in fruits.enumerated() { print("\(index+1). \(fruit)") } |
Chapter 8: Optionals – The “Maybe” Type
This is the most important Swift concept!
|
0 1 2 3 4 5 6 7 8 |
var phoneNumber: String? = "9876543210" // maybe has value, maybe nil phoneNumber = nil // perfectly ok |
Danger:
|
0 1 2 3 4 5 6 7 |
// This CRASHES if nil! print(phoneNumber.count) // ← never do this! |
Safe ways:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// 1. If let (most common) if let number = phoneNumber { print("Number: \(number)") } else { print("No number saved") } // 2. Default value let display = phoneNumber ?? "Not set" print(display) // Modern short version if let phoneNumber { print("You have a number!") } |
Chapter 9: Functions – Reusable Code
|
0 1 2 3 4 5 6 7 8 9 10 |
func greet(name: String) { print("Namaste, \(name)! 🌼") } greet(name: "Sourav") |
With return:
|
0 1 2 3 4 5 6 7 8 9 10 |
func add(a: Int, b: Int) -> Int { return a + b } let sum = add(a: 10, b: 20) // 30 |
Default parameters:
|
0 1 2 3 4 5 6 7 8 9 10 |
func introduce(name: String, city: String = "Hyderabad") { print("\(name) from \(city)") } introduce(name: "Priya") // Priya from Hyderabad |
Summary Table – What You Learned Today
| Topic | Key Idea | Example Code |
|---|---|---|
| Variables | let vs var | let name = “Amit” |
| Types | Int, Double, Bool, String | let price = 99.99 |
| Strings | Interpolation, methods | “\(first) \(last)” |
| Control Flow | if, switch | if temp > 30 { … } |
| Arrays | Ordered list | fruits.append(“Orange”) |
| Optionals | ?, ??, if let | var age: Int? |
| Functions | Reusable blocks | func greet(name: String) |
Common Mistakes Beginners Make (and How to Avoid)
| Mistake | Wrong Code | Correct Way |
|---|---|---|
| Using var everywhere | var name = “Rahul” | let name = “Rahul” |
| Force-unwrapping optionals | phoneNumber! | if let phoneNumber { … } |
| Forgetting label in function call | greet(“Amit”) | greet(name: “Amit”) |
| Ignoring type inference | let age: Int = 25 (unnecessary) | let age = 25 |
Homework (Try These!)
- Create a playground and print your name, city, and favorite Tagore poem line.
- Write a function that takes temperature and returns a message (“Hot”, “Nice”, “Cold”).
- Make an array of 5 Bengali sweets and loop through them printing “I like [sweet]!”.
- Create an optional variable age: Int? = nil and safely print it using if let and ??.
Paste your code here if you want me to review or fix errors — I’ll help instantly!
Next lesson? We can go to:
- Enums (powerful in Swift!)
- Structs (your own data types)
- First SwiftUI app (make a real button!)
- Optionals + arrays deeper
- Or anything you like!
Just say what feels most exciting 😊 Ready for the next chapter?
