Swift
201 bites tagged Swift — interview questions with model answers, and 60-second explainers.
Creating a native module with a sync method
Extend the RN base class, mark the method exported and synchronous, register in a package or bridging file. native module mechanics across platforms.
XCFrameworks and Binary Distribution Trade-offs
XCFramework bundles slices for multiple platforms/architectures, used to hide source or speed builds. knowledge of binary distribution on Apple platforms. ignoring debugging, Swift ABI, and update friction trade-offs.
Decoupled Cross-Module Navigation in Modular iOS
Depend on a shared abstraction, register concrete routers in a coordinator or registry, resolve at runtime. decoupling feature modules. importing module B directly or coupling features through a god object.
Resolving Conflicting Transitive Versions in SPM
SPM allows only one version per package, so diagnose the graph, then update, pin, or fork to find an overlap. understanding of SPM's single-version resolution. assuming both versions can coexist like in some other ecosystems.
Making a Legacy UIViewController Testable
Extract logic into a testable view model, inject dependencies, keep the controller a thin humble object. refactoring legacy UIKit for testability. rewriting everything at once or testing through the UIKit lifecycle directly.
setUp() and tearDown() in XCTestCase
SetUp builds fresh state before each test, tearDown cleans up after, ensuring independence. knowledge of XCTest lifecycle and test isolation. thinking they run once per class or sharing mutable state across tests.
Generating Video Thumbnails with AVAssetImageGenerator
Load AVAsset, configure AVAssetImageGenerator, request the time off the main thread, hop back to update UI. AVFoundation usage plus async UI hygiene. doing the decode synchronously on the main thread.
Building a Card Flip Animation in SwiftUI
Drive a flipped state, rotate both faces with rotation3DEffect, hide the back face. SwiftUI animation and 3D transform fluency. forgetting the back is mirrored or showing both faces at once.
Push vs Modal Presentation in UIKit
Push adds to a nav stack with back, modal presents a self-contained interrupting flow. understanding of UIKit navigation models. treating them as interchangeable or ignoring the stack relationship.
IBOutlet vs IBAction in Interface Builder
Outlet references a view, action handles an event, both connect via Ctrl-drag. grasp of Storyboard-to-code wiring. confusing the two directions or forgetting the connection is runtime, not compile-time.
Protocols with associated types and opaque types
A PAT leaves a placeholder type unresolved, so it has no single concrete shape and historically could not be used as a bare existential; opaque types with some let you return one fixed underlying conforming… generics and type erasure.
do-try-catch versus Optional return types
Throwing functions with do/try/catch convey why something failed via typed Error values; Optionals only say success or absence. Use throws when the caller needs the reason. error modeling choices.
Closures and trailing closure syntax
Closures are self-contained function blocks that capture surrounding state; a final closure parameter can be passed as a trailing closure outside the parentheses. first-class functions and capture.
Purpose of the guard statement
Guard requires its condition to hold or else exits the scope, and unwrapped optionals stay in scope afterward; this flattens nesting versus if. early-exit control flow.
Retain cycles and how to break them
A cycle is two objects (or a closure and its owner) holding mutual strong references so neither deallocates; break it with a [weak] or [unowned] capture list. ARC and closure capture.
Decoding a heterogeneous JSON array by a type field
Peek the type field with a partial decode, switch on it, and decode the concrete type, wrapping each in an enum or boxed protocol value. custom Decodable with a discriminator.
Copy-on-write and implementing it for a custom struct
CoW shares a buffer until mutation, deep-copying only when the buffer is non-unique; Array, Dictionary, Set, String use it; implement with a class storage box and isKnownUniquelyReferenced. value semantics with shared backing storage.
map versus compactMap versus flatMap
Map transforms each element one-to-one; compactMap transforms then drops nils; flatMap transforms to sequences then concatenates. functional transforms on sequences.
Verifying and avoiding copy-on-write overhead
Confirm extra copies via isKnownUniquelyReferenced or instruments and the retain trace; ensure unique references, mutate inout/in place, reserveCapacity, avoid aliasing. CoW mechanics and mutation patterns.
The dynamic keyword and message dispatch
Dynamic forces Objective-C message dispatch via objc_msgSend, enabling KVO and swizzling, at the cost of skipping inlining and devirtualization. dispatch mechanisms and ObjC runtime.
dispatch_async versus Task.detached for offloading work
Dispatch_async submits a closure to a queue; Task.detached starts an unstructured async task off the current actor without inheriting context. GCD versus structured concurrency.
What an autoreleasepool is and when to add one manually
An autorelease pool holds objects until it drains; wrap tight loops creating many temporary ObjC objects in autoreleasepool to cap peak memory. deferred deallocation under tight loops. thinking it relates to ARC retain cycles.
Exposing Swift classes to Objective-C
Subclass NSObject or mark members @objc, import the generated ModuleName-Swift.h, and accept that Swift-only types like enums with associated values cannot cross. Swift-to-ObjC interop rules.
Struct versus class performance and memory tradeoffs
Structs are value types, often stack-allocated with no ARC; classes are heap-allocated reference types with retain counts. value versus reference semantics and allocation cost. claiming structs are always faster.
Get Swift bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.