🦋 Swift — Learn From Zero
Playground: Swift Playgrounds | Replit Swift — click, type, run.
What is Swift?
Swift is Apple's fast, safe language for iOS, macOS, and server-side. Modern syntax with strong type safety.
- iOS & macOS apps
- Server (Vapor)
- Systems scripting
- ML with Core ML
Run Online — No Install
Use the playground links above.
🟢 Beginner — Core Basics
Your First Program
swift
print("Hello, world!")No main needed in a playground.
Variables & Types
swift
let name = "Ada"
var age = 36
let height: Double = 1.78Use let (constant) by default.
If / Else
swift
let age = 18
if age >= 18 {
print("Vote")
} else {
print("Too young")
}Conditionals don't need parens.
Loops
swift
for i in 0..<5 { print(i) }
for fruit in ["apple", "banana"] { print(fruit) }Use 0..<5 (half-open).
Functions
swift
func greet(_ name: String) -> String {
return "Hello, \(name)!"
}String interpolation with \().
Optionals
swift
var s: String? = nil
if let val = s { print(val) }
print(s?.count ?? 0)? marks optional; unwrap safely.
🟡 Intermediate — Building Real Programs
Structs & Classes
swift
struct Person {
var name: String
var age: Int
}Value types (struct) vs reference (class).
Protocols
swift
protocol Animal { func speak() }
struct Dog: Animal { func speak() {} }Protocols are interfaces.
Closures
swift
let add: (Int, Int) -> Int = { $0 + $1 }
print(add(2, 3))Trailing closures & shorthands.
Enums
swift
enum Direction { case north, south }
let d = Direction.north
switch d { case .north: break; default: break }Powerful enums with associated values.
Collections
swift
let nums = [1, 2, 3]
let even = nums.filter { $0 % 2 == 0 }Functional methods on collections.
🔴 Advanced — Pro Concepts
async/await
swift
func load() async -> String {
try? await Task.sleep(nanoseconds: 1_000_000_000)
return "x"
}Swift Concurrency model.
Generics
swift
func first(_ arr: [T]) -> T? { arr.first } Type parameters.
Property Wrappers
swift
@propertyWrapper
struct Clamped { var wrappedValue: Int { didSet { } } }Reusable property logic.
Actors
swift
actor Bank {
var balance = 0
func deposit(_ n: Int) { balance += n }
}Data-race safety for concurrency.
What's Next?
- Build a tiny project: Calculator, To-Do App, Weather Widget
- Read the official docs and a good book
- Solve Exercism exercises
- Join a community & share your code
Practice daily for 20 minutes. Consistency > intensity.