Lesson 25 / 25

Revision and Interview Questions

Recall Swift concepts quickly for exams and interviews.

Cheat sheet

Basics: let vs var; type inference; no implicit numeric conversion; overflow traps; Unicode-correct String with String.Index; tuples. Optionals: ?, if let, guard let with early exit, ??, optional chaining, avoid !. Control flow: ranges, exhaustive switch without fall-through, patterns with where, if/switch expressions, defer. Collections: Array, Dictionary, Set as value types with copy-on-write; map, filter, reduce, compactMap, grouping. Types: structs (value semantics, memberwise init, mutating) by default; enums with raw and associated values, CaseIterable; classes for identity and inheritance, final, failable init, deinit, ===. Functions: argument labels, defaults, variadics, inout with &; closures, trailing closures, $0, @escaping, capture lists. Errors: throws, try, do/catch, try?, try!, typed throws, Result. Protocols: extensions with default implementations, synthesised Equatable/Hashable/Codable, generics with constraints, associated types, some vs any. Memory: ARC, strong/weak/unowned, retain cycles. Access: private, fileprivate, internal (default), package, public, open. Concurrency: async/await, async let, task groups, Task, cooperative cancellation, actors, @MainActor, Sendable, Swift 6 data-race safety, AsyncSequence. Tools: SwiftPM, Swift Testing (@Test, #expect), XCTest.

Common interview questions

Answer each with a short code example.

1. What is an optional, and what are the safe ways to unwrap one?
2. Struct vs class: differences, and when would you choose each?
3. What is copy-on-write?
4. Explain enums with associated values with a real example.
5. What is a retain cycle? How do weak and unowned differ?
6. What does @escaping mean, and why does [weak self] matter?
7. Explain protocol extensions and default implementations.
8. some vs any: what is the difference?
9. How does Swift error handling differ from exceptions? What are typed throws?
10. Explain async/await, async let and task groups.
11. What problem do actors solve, and what is @MainActor?
12. What does Swift 6's data-race safety check?

Explain value semantics confidently

Many Swift interview questions come back to value versus reference semantics: structs vs classes, copy-on-write, actors and Sendable. Being able to explain why value types make concurrency safer is a strong signal.

Quick check: Which keyword marks a class reference that does not keep the object alive and becomes nil automatically?

  • strong
  • unowned
  • lazy
  • weak
Answer

weak — weak references are zeroing and do not increase the reference count.