Swift language interview questions
Optionals, value semantics, protocols, generics, closures, error handling and the modern Swift features interviews test.
– interview-ready · 23 questions in this topic
– of 23 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
- Optionals and why Swift has themPredict
- Value types vs reference typesPredict
- Protocols vs abstract classesPick the fix
- Closures and capture listsPredict
- @escaping vs non-escaping closuresPick the fix
- GenericsPick the fix
- Associated types and primary associated typesPick the fix
- Enums with associated and raw valuesPredict
- willSet and didSetPredict
- Stored, computed and lazy propertiesPredict
- The defer statementPredict
- Result and typed throwsPick the fix
- inout parametersPredict
- Key pathsPick the fix
- Opaque types (some) vs existentials (any)Pick the fix
- @autoclosurePredict
- Copy-on-writePredict
- Property wrappersFill the blank
- == vs ===Predict
- Swift macrosPick the fix
- Phantom typesPick the fix
- HashableSpot the bug
- Noncopyable types (~Copyable)Pick the fix
Practise this topic
Flashcards · 31
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 23 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 4 sets
Timed, out loud, then tick what you covered. Needs JavaScript.
Fix this PR · 1 review
Review a diff like a senior: flag the lines you'd comment on. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
let vs var
letbinds a constant andvara variable. Prefer let; the compiler warns when var is never mutated.Subscripts
subscript(index:) -> Tadds[]access to your types, with optional setters and multiple parameters.@discardableResult
Silences the unused-result warning. Use it only when ignoring the result is normal (like
removeFirst), not to hide errors.Nominal typing
Swift types are identified by name, not shape: two structs with the same fields are different types. Tuples and functions are structural.
Never
A function returning
Neverdoesn't return (fatalError, preconditionFailure). The compiler knows code after it is unreachable.@dynamicMemberLookup
Lets
obj.anythingcompile by routing to asubscript(dynamicMember:), often with key paths for type safety.Any vs AnyObject
Any holds any value, including structs and functions; AnyObject holds only class instances.
class vs static methods
Both belong to the type.
staticcan't be overridden;class(classes only) can be overridden by subclasses.
Printable cheat sheet · Review your weak spots in this topic