Swift Tutorial
Swift Tutorial – Like a Real Teacher Would Explain It (2026 style)
Lesson 1 – Getting Started – Where do I even write code?
Three realistic choices right now:
| Option | Do you need a Mac? | Best for beginners? | Speed to see first result | My recommendation |
|---|---|---|---|---|
| Swift Playgrounds (app) | Yes (Mac or iPad) | ★★★★★ | 20 seconds | Best starting point |
| Online playground | No | ★★★★ | 10 seconds | Very good if no Mac |
| Xcode | Yes | ★★★ | 2–5 minutes | After 1–2 weeks |
| VS Code + Swift | Any computer | ★★ | 10–30 minutes setup | Later (scripting) |
Today I recommend: Open Swift Playgrounds on Mac/iPad OR go to https://swiftfiddle.com (online, no installation)
Just press the + button → choose Blank playground → and you’re ready.
Let’s write our first line.
|
0 1 2 3 4 5 6 |
print("Hello friend!") |
Run it (▶️ button or Cmd+R).
You should see:
|
0 1 2 3 4 5 6 |
Hello friend! |
That’s your first program. 🎉
Lesson 2 – Variables and Constants (the most important first concept)
Think of variables like labeled boxes.
You can put something in the box, and later change what’s inside.
|
0 1 2 3 4 5 6 7 8 |
var age = 22 // I can change this later age = 23 // ok age = age + 1 // now 24 |
But sometimes you want a value that should never change — like your birth year, your username, etc.
That’s what let is for:
|
0 1 2 3 4 5 6 7 |
let birthYear = 2001 // birthYear = 2002 ← this will NOT compile → good! |
Rule most beginners forget:
- Use letby default
- Use varonly when you know you will change the value
Bad habit example (very common):
|
0 1 2 3 4 5 6 7 |
var name = "Sara" // ← should be let ! name = "Sara Smith" // actually never changed again |
Better:
|
0 1 2 3 4 5 6 |
let name = "Sara Smith" |
Lesson 3 – Basic Types (what kinds of things can live in boxes)
| Type | Example values | Real-life analogy | Most common? |
|---|---|---|---|
| Int | 0, -5, 42, 1000000 | Number of apples, age, ID | Yes |
| Double | 3.14, -0.001, 99.99 | Money, height, temperature | Yes |
| Bool | true, false | Light switch (on/off) | Yes |
| String | “hello”, “😺”, “” | Text, names, messages | Very yes |
| Character | “A”, “🚀”, ” “ | Single letter/symbol | Rarely |
Examples:
|
0 1 2 3 4 5 6 7 8 9 10 |
let age = 27 // Int let height = 1.76 // Double let isStudent = true // Bool let username = "dark_knight42" // String let favoriteEmoji : Character = "⚡" // rare but ok |
Swift usually guesses the type (type inference) — this is good!
But sometimes you want to be very clear:
|
0 1 2 3 4 5 6 7 |
let score: Int = 100 let price: Double = 19.99 |
Lesson 4 – Strings – You will use them every day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
let first = "Emma" let last = "Wilson" // Classic way (old-school) let full = first + " " + last // "Emma Wilson" // Modern & best way (99% of the time) let full2 = "\(first) \(last)" // "Emma Wilson" // Very long text (multi-line) let message = """ Hello! I hope you're having a great day. Write me back soon 😊 """ |
Useful things you do with strings all the time:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
let text = "Swift is actually quite nice!" print(text.count) // 31 print(text.uppercased()) // SWIFT IS ACTUALLY QUITE NICE! print(text.lowercased()) // swift is actually quite nice! print(text.hasPrefix("Swift")) // true print(text.hasSuffix("nice!")) // true print(text.contains("quite")) // true |
Lesson 5 – Doing decisions – if and switch
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
let temperature = 28 if temperature >= 30 { print("It's really hot 🥵") } else if temperature >= 22 { print("Nice weather 😎") } else if temperature >= 10 { print("A bit chilly 🧥") } else { print("Freezing! 🥶") } |
Very important tip: Put the most common case first — code is easier to read.
Now — switch (very loved in Swift):
|
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("Excellent! 🎉") case "B": print("Good job") case "C": print("You passed") case "D", "F": print("We need to talk...") default: print("Invalid grade 🤔") } |
Modern pattern you will see a lot:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let status = 404 switch status { case 200..<300: print("Everything ok") case 400..<500: print("You did something wrong") case 500...: print("Server is crying") default: print("Weird status code") } |
Lesson 6 – 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 |
// Creating var friends = ["Mia", "Leo", "Zoe", "Noah"] // Reading print(friends[0]) // Mia print(friends[2]) // Zoe // Adding friends.append("Ava") // at the end friends.insert("Liam", at: 0) // at the beginning // Removing friends.remove(at: 1) // removes Leo friends.removeLast() // removes last person // Checking if friends.contains("Zoe") { print("Zoe is still here!") } |
Looping over array (three common ways):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Way 1 – simplest for friend in friends { print("Hello \(friend)!") } // Way 2 – with number for (index, friend) in friends.enumerated() { print("\(index+1). \(friend)") } // Way 3 – only when you need index for i in 0..<friends.count { print(friends[i]) } |
Lesson 7 – Optionals – The part that confuses everyone at first
Real-life analogy:
Imagine you ask: “Do you have a nickname?”
- Some people say “yes, it’s Sunny” → has value
- Some people say “no” → nil (no nickname)
In Swift we write:
|
0 1 2 3 4 5 6 7 8 9 |
var nickname: String? = "Sunny" // Later... nickname = nil // now they have no nickname |
You cannot just use it directly!
|
0 1 2 3 4 5 6 7 |
// This CRASHES if nickname is nil print(nickname.count) // ← dangerous! |
Safe ways (choose these):
Way 1 – if let (most common)
|
0 1 2 3 4 5 6 7 8 9 10 |
if let nick = nickname { print("Your nickname is \(nick)") } else { print("No nickname set") } |
Modern short version (very popular now):
|
0 1 2 3 4 5 6 7 8 |
if let nickname { print("Hi \(nickname)!") } |
Way 2 – default value
|
0 1 2 3 4 5 6 7 |
let display = nickname ?? "Friend" print("Hello \(display)") |
Way 3 – only when 100% sure (dangerous – use rarely)
|
0 1 2 3 4 5 6 |
let forced = nickname! // crashes if nil |
|
0 1 2 3 4 5 6 7 8 9 10 11 |
func sayHello(to name: String) { print("Hey \(name), how are you?") } sayHello(to: "Julia") sayHello(to: "Mateo") |
Functions that return something:
|
0 1 2 3 4 5 6 7 8 9 10 |
func square(of number: Int) -> Int { return number * number } let result = square(of: 8) // 64 |
Default values:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func greet(name: String, politely: Bool = true) { if politely { print("Dear \(name), welcome!") } else { print("Yo \(name)!") } } greet(name: "Sara") // polite version greet(name: "Max", politely: false) |
Where are we now?
You already understand ~70% of lines in most beginner/intermediate Swift code!
Checklist:
- let vs var
- Basic types (Int, Double, Bool, String)
- String interpolation + methods
- if, else, switch
- Arrays + looping
- Optionals (?, ??, if let)
- Functions
Next steps – pick one path:
- SwiftUI → make real iPhone/iPad apps (most popular)
- Enums + Structs + Protocols → better organize data
- Closures → very important modern Swift feature
- async/await → modern way to do network, files, delays
- Small project → Todo list, quiz game, unit converter…
Which direction would you like to continue with?
Tell me what feels most interesting to you right now 😊
