Swift – Modern Crash Course (2026 Edition)
0. First Things First – How to Actually Run Swift Code
Three realistic ways in 2025/2026:
| Way | Best for | Speed to first line | Recommendation for beginners |
|---|---|---|---|
| Xcode (Mac) | Serious iOS/macOS apps | Medium | ★★★ (long-term best) |
| Swift Playgrounds | Learning, small experiments | Very fast | ★★★★★ (start here!) |
| Online playgrounds | No Mac computer | Instant | ★★★★ (good backup) |
| VS Code + Swift extension | General scripting & backend | Medium | ★★★ (later stage) |
Quick start recommendation for most people in 2025:
→ Open Swift Playgrounds (free on Mac/iPad) → Or go to https://swiftfiddle.com or https://www.swift.org/playground/
Let’s begin!
1. Hello World & Basics (5 minutes)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 |
print("Hello, world!") // This is a comment /* Multi-line comment looks like this */ |
Variables & Constants
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let name = "Alex" // constant (cannot change) var age = 24 // variable (can change) age = 25 // ✅ ok // name = "Bob" // ❌ compile error! let cool = true // Bool let height = 1.78 // Double (most common floating point) let starsInSky = 987654321234567 // Int can be very large |
Type inference is super strong in Swift → you usually don’t write the type.
But you can write it:
|
0 1 2 3 4 5 6 7 8 9 |
let score: Int = 100 let temperature: Double = 23.5 let isRaining: Bool = false let emoji: Character = "🚀" |
2. Strings – You’ll use them A LOT
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let firstName = "Emma" let lastName = "Wilson" // Concatenation (old school) let fullName1 = firstName + " " + lastName // Modern & recommended way (2025 style) let fullName2 = "\(firstName) \(lastName)" // Multi-line strings let story = """ It was a dark and stormy night... ...or was it just my code not compiling again? """ |
Useful string things:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
let text = "Swift is awesome 😎" print(text.count) // 19 print(text.uppercased()) // SWIFT IS AWESOME 😎 print(text.hasPrefix("Swift")) // true print(text.hasSuffix("some 😎")) // false print(text.contains("awesome")) // true |
3. Control Flow – if, switch, loops
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let temperature = 28 if temperature > 30 { print("It's too hot ☀️") } else if temperature > 20 { print("Nice weather 🌤️") } else { print("Better wear a jacket 🧥") } |
Switch – very powerful in Swift
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let direction = "north" switch direction { case "north": print("Go up ↑") case "south": print("Go down ↓") case "east", "west": // multiple values print("Horizontal move") default: print("Unknown direction 🤔") } |
Very common modern pattern (2026+):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let statusCode = 404 switch statusCode { case 200..<300: print("Success 🎉") case 400..<500: print("Client error 😕") case 500...: print("Server broke 💥") default: print("Weird status") } |
4. Arrays & Dictionaries (Collections)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Array var names = ["Anna", "Ben", "Clara", "David"] names.append("Emma") // add at the end names.insert("Zoe", at: 0) // insert at index 0 names.remove(at: 2) // remove Clara print(names[1]) // Ben print(names.count) // 4 // Check if contains if names.contains("Emma") { print("Emma is here!") } |
Dictionary (key → value)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var scores = [ "Anna": 95, "Ben": 82, "Clara": 100 ] scores["David"] = 67 // add new scores["Ben"] = 88 // update scores["Zoe"] = nil // remove if let score = scores["Anna"] { print("Anna scored \(score)") // Anna scored 95 } |
Looping (most common patterns)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
for name in names { print(name) } for (name, score) in scores { print("\(name) → \(score)") } // With index for (index, name) in names.enumerated() { print("\(index+1). \(name)") } |
5. Optionals – The thing that scares everyone at first
|
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 27 |
var nickname: String? = "Sunny" // can be String or nil nickname = nil // perfectly legal // BAD – will crash if nil! // print(nickname.count) // Safe ways (choose one): // 1. Optional binding (most common) if let nick = nickname { print("Nickname is \(nick)") } else { print("No nickname") } // 2. Nil-coalescing let displayName = nickname ?? "Anonymous" print(displayName) // Anonymous if nickname was nil // 3. Force unwrap (only when 100000% sure) let forced = nickname! // crashes if nil → use rarely |
Modern short syntax (very popular 2024–2026):
|
0 1 2 3 4 5 6 7 8 |
if let nickname { print("Hello \(nickname)") } |
6. Functions – Make your code reusable
|
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 |
func greet(name: String) { print("Hi \(name)! 👋") } greet(name: "Julia") // With return func square(number: Int) -> Int { return number * number } let result = square(number: 7) // 49 // Multiple parameters + default value func introduce(name: String, age: Int = 20, city: String = "Unknown") { print("\(name) is \(age) years old from \(city)") } introduce(name: "Leo") // age & city default introduce(name: "Mia", age: 19, city: "Berlin") |
7. Structs – Your own data types (very important!)
|
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 27 |
struct Player { let id: Int var name: String var score: Int = 0 var isActive = true // computed property var displayName: String { isActive ? name : "\(name) (offline)" } // method mutating func addPoints(_ points: Int) { score += points } } var player1 = Player(id: 1, name: "Alex") player1.addPoints(50) print(player1.score) // 50 print(player1.displayName) // Alex |
Quick Summary – Where You Are Now
You already know enough to read & understand ~70% of real Swift code!
What you know:
- Variables / constants
- Strings, numbers, booleans
- if / switch
- Arrays & Dictionaries
- Optionals (? and ?? and if let)
- Functions
- Structs (your own types)
Next logical steps (choose your path):
| Path | What you’ll learn next | Goal after 2–4 weeks |
|---|---|---|
| iOS / SwiftUI | Views, @State, NavigationStack, List, Sheet | Build real iPhone apps |
| Server / Backend | Vapor / Hummingbird, async/await, Codable, APIs | Build REST APIs |
| Swift scripting | FileManager, ArgumentParser, regex, JSON | Write useful command-line tools |
| Game / Graphics | SpriteKit / SceneKit / Metal / RealityKit | Make 2D/3D games & AR experiences |
Would you like to continue with one of these paths?
- SwiftUI – make iPhone apps (most popular choice)
- Server-side Swift (Vapor / APIs)
- More fundamentals (enums, protocols, closures, async/await)
- Small real project (todo app, weather cli, quiz game…)
Just tell me which direction feels most exciting to you right now 😄
