Swift Essentials
The Swift language for iOS: optionals, structs vs classes, protocols, closures, enums and error handling.
Beginner · about 10 hours · difficulty 2/5
If you chose native iOS, Swift is your language. It favours safety and value types, and SwiftUI is built entirely on its features.
What to learn
- let vs var, type inference.
- Optionals:
String?, unwrapping withif let,guard letand??. Avoid force unwrapping with!. - Structs vs classes: structs are copied (value semantics) and are the default in Swift; classes are shared references. SwiftUI views are structs.
- Enums with associated values model states like
.loading,.loaded([Item]),.failed(Error). - Protocols and extensions:
Identifiable,CodableandEquatableappear everywhere. - Closures and trailing closure syntax.
- Error handling with
throws,tryanddo/catch.
Concurrency preview
Swift has async/await and actors built in. @MainActor marks code that must run on the main thread, which is where UI updates happen.
Pitfalls
Retain cycles between classes and closures leak memory; use [weak self] where a closure outlives its owner.
Good enough to move on: you can decode JSON into a Codable struct and model a screen's state with an enum.
Key concepts
- let, var and type inference
- Optionals and unwrapping
- Structs vs classes
- Enums with associated values
- Protocols, extensions and Codable
- Closures
- Error handling
- Memory management and ARC
Mini project
In a Swift playground, decode a JSON array of movies into Codable structs and print the titles of those rated above 8.
Check yourself
Why are SwiftUI views structs rather than classes, and what does value semantics buy you?
Free resources
- The Swift Programming Language (Swift.org)
- Swift.org documentation (Swift.org)