Chapter 2: Swift Get Started
Swift – Get Started (Very Detailed Beginner Lesson)
0. Where should I write and run Swift code today?
You have these realistic choices right now (2025–2026):
| Method | Needs Mac? | First line in… | Best for right now? | Recommendation |
|---|---|---|---|---|
| Swift Playgrounds (app) | Yes | ~15 seconds | ★★★★★ | Best starting point |
| Online playground | No | ~10 seconds | ★★★★ | Very good backup |
| Xcode | Yes | 2–5 minutes | ★★★ | After 1–2 weeks |
| VS Code + Swift extension | Any | 15–40 min setup | ★★ | Later (scripting) |
Strong recommendation for the next 1–2 weeks:
→ Open Swift Playgrounds on Mac or iPad OR go to https://swiftfiddle.com (online, zero installation)
Both let you write and run code instantly.
Let’s begin!
1. Your very first line
Create a new blank playground and write:
|
0 1 2 3 4 5 6 |
print("Hello from my first Swift line!") |
Run it (press the play button ▶️ or Cmd+R in Xcode/Playgrounds).
You should see:
|
0 1 2 3 4 5 6 |
Hello from my first Swift line! |
That’s it — you just executed Swift code. 🎉
2. Comments — explaining your code to future you
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// This is a single-line comment /* This is a multi-line comment that can span several lines */ // Most developers use // for almost everything |
Good habit: write small comments explaining why you did something — not just what.
3. Naming things — variables & constants
Think of variables as named boxes.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
// I can change the content later → use var var age = 24 age = 25 age = age + 1 // now 26 // I promise I will NEVER change this → use let let birthYear = 2001 // birthYear = 2002 ← this line will NOT compile (good!) |
Very important rule most beginners break at first:
Use let by default — only use var when you are sure you will change the value.
Common beginner mistake:
|
0 1 2 3 4 5 6 7 |
var username = "priya_tech" // ← should be let ! var email = "priya@example.com" // ← should be let ! |
Better:
|
0 1 2 3 4 5 6 7 |
let username = "priya_tech" let email = "priya@example.com" |
4. Most common types you’ll meet every day
| Type | Examples | Real-life meaning | Default choice? |
|---|---|---|---|
| Int | 0, 42, -17, 1000000 | Age, count, ID, quantity | Yes |
| Double | 3.14, 1.78, -0.05, 99.99 | Height, price, temperature, percentage | Yes |
| Bool | true, false | Is raining? Is logged in? Is adult? | Yes |
| String | “Hello”, “কলকাতা”, “🚀”, “” | Name, message, address, label | Yes |
Examples:
|
0 1 2 3 4 5 6 7 8 9 |
let peopleInRoom = 7 // Int let currentTemperature = 28.4 // Double let isRaining = false // Bool let city = "Hyderabad" // String |
You usually don’t need to write the type — Swift guesses it very well.
But you can write it when you want to be super clear:
|
0 1 2 3 4 5 6 7 |
let score: Int = 95 let price: Double = 499.99 |
5. Strings — you will use them constantly
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
let firstName = "Anirudh" let lastName = "Sharma" // Modern & best way (string interpolation) let fullName = "\(firstName) \(lastName)" // "Anirudh Sharma" // Old-school way let fullOld = firstName + " " + lastName // Very long text let aboutMe = """ I'm learning Swift. It's actually quite fun once you get past optionals. I love Hyderabad biryani too! 🍲 """ |
Useful things you do 100 times a day:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
let message = "Swift is modern and safe!" print(message.count) // 28 print(message.uppercased()) // SWIFT IS MODERN AND SAFE! print(message.lowercased()) // swift is modern and safe! print(message.hasPrefix("Swift")) // true print(message.hasSuffix("safe!")) // true print(message.contains("modern")) // true |
6. Doing decisions — if statement
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
let temperature = 33 if temperature > 35 { print("It's dangerously hot 🥵") } else if temperature > 28 { print("Typical Hyderabad summer 🌞") } else if temperature > 20 { print("Nice weather 😊") } else { print("Chai weather ☕") } |
Tip: Write the most common situation first — code becomes easier to read.
7. Doing many decisions — switch
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let grade = "A" switch grade { case "A", "A+": print("Outstanding! 🌟") case "B": print("Good work") case "C": print("You passed") case "D", "F": print("We need to talk...") default: print("Unknown grade") } |
Very common modern pattern:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let httpCode = 404 switch httpCode { case 200..<300: print("Success 🎉") case 400..<500: print("You made a mistake 😕") case 500...: print("Server is having a bad day 💥") default: print("Strange code") } |
8. Lists of things — Arrays
|
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 25 26 |
// Create var friends = ["Riya", "Arjun", "Sneha"] // Add friends.append("Karan") // Read print(friends[0]) // "Riya" print(friends[2]) // "Sneha" // Change friends[1] = "Aarav" // Remove friends.remove(at: 0) // removes Riya friends.removeLast() // removes last person // Check if friends.contains("Sneha") { print("Sneha is still in the group!") } |
Most common loop:
|
0 1 2 3 4 5 6 7 8 |
for friend in friends { print("Hello \(friend)! 👋") } |
With numbering (very common):
|
0 1 2 3 4 5 6 7 8 |
for (index, friend) in friends.enumerated() { print("\(index + 1). \(friend)") } |
9. The most important concept in Swift: Optionals
Real-life analogy: You ask someone: “Do you have a nickname?”
- Sometimes they say “Yes — Sunny”
- Sometimes they say “No” (nothing / nil)
In Swift:
|
0 1 2 3 4 5 6 7 8 9 |
var nickname: String? = "Sunny" // Later... nickname = nil // legal — they have no nickname now |
Danger — you cannot just use it:
|
0 1 2 3 4 5 6 7 |
// THIS CRASHES if nickname is nil print(nickname.count) // ← never write this! |
Safe ways (these are the correct ones):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Way 1 — most common (if let) if let nick = nickname { print("Nickname: \(nick)") } else { print("No nickname") } // Way 2 — modern short version (very popular now) if let nickname { print("Hi \(nickname)!") } // Way 3 — provide a default let displayName = nickname ?? "Anonymous" print(displayName) // Way 4 — only when you are 1000000% sure (dangerous!) let forced = nickname! // crashes if nil — use very rarely |
Quick Recap Table – What You Already Know
| Concept | Main idea | Most common way to write it |
|---|---|---|
| Constant | Never changes | let name = “Amit” |
| Variable | Can change | var count = 0 |
| String | Text | “Hello \(name)” |
| Decision | Choose different path | if temperature > 30 { … } |
| Multiple choices | Many clean cases | switch status { case 200..<300: … } |
| List | Ordered collection | var items = [“apple”, “banana”] |
| Maybe-value | Can be value or nothing | var email: String? + if let email { … } |
Your First Mini-Exercise (try these right now)
Copy-paste each one into a playground and run:
- Print your name and city using string interpolation
|
0 1 2 3 4 5 6 7 8 |
let name = "???" let city = "???" print("I am \(name) from \(city)") |
- Check temperature and print message
|
0 1 2 3 4 5 6 7 8 9 10 11 |
let temp = 31 if temp > 32 { print("Too hot!") } else { print("Manageable") } |
- Make a list of 4 favorite foods and print them numbered
|
0 1 2 3 4 5 6 7 |
let foods = ["???", "???", "???", "???"] // your loop here |
- Create an optional variable and print it safely
|
0 1 2 3 4 5 6 7 |
var mood: String? = "happy" // safe print here using if let or ?? |
Next logical step — which one feels most interesting to you?
- Optionals in more depth (very important)
- Functions — make reusable code blocks
- Structs — create your own data types (like a Person, Book, Order…)
- SwiftUI — make your first real button & text on screen
- Loops + arrays more deeply
Just tell me which direction you want to go next — I’ll continue in the same detailed, teacher-like style 😊
