Swift
201 bites tagged Swift — interview questions with model answers, and 60-second explainers.
Swift Properties: Accessors in Disguise
A Swift property runs code on every read or write. Use stored properties for state, computed properties for derived values, and observers for side effects. The footgun is heavy work in a computed getter, turning innocent dot syntax into a performance trap.
Swift Closures: Functions with a Memory
A closure is a function carrying luggage: it captures surrounding variables and remembers them later. Use them in SwiftUI actions, async completions, and array transforms. Beware a strong reference cycle from capturing self without a capture list.
Swift Collections: Array, Set, Dictionary
Swift's three collections are different access patterns, not just different APIs. Arrays keep order, Sets enforce uniqueness, and Dictionaries map keys to values. Picking an Array for uniqueness checks turns membership from O(1) into O(n) scans.
Swift Basic Types: Value Semantics by Default
Swift's basic types are value type structs, so assignment copies, not shares, a reference. You feel this when passing Strings into functions or choosing Int over Double. The footgun is treating them as free to copy; large values cost memory and speed.
Apple's Natural Language Framework
Apple's Natural Language Framework turns raw strings into structured meaning on-device without network calls. Tokenize queries or extract entities from user text locally. It is not infallible; heavy synchronous tagging on the main thread freezes your UI.
UserNotifications Framework: iOS Alert Gatekeeper
UserNotifications is the gatekeeper between your app and the lock screen. You use it to request permission and schedule local or push alerts on iOS. Send a notification before authorization and the system silently drops it.
Result Builders: Declarative Swift DSLs
Result builders teach the compiler to fold expressions into one combined value, enabling declarative DSLs. SwiftUI relies on them to compose stacked views. Type errors surface on compiler-generated boilerplate instead of your original code.
Swift Enums: Type-Safe Choice Modeling
A Swift enum is a closed menu of possibilities the compiler tracks exhaustively. Use it to replace string constants or model a network result state. Adding a case without updating every switch breaks compile-time safety if you rely on a default clause.
The Package.swift Manifest File
Think of Package.swift as a recipe for your code, telling the Swift Package Manager (SPM) what to build, what it needs, and where it can run. You use it to define libraries, list dependencies, and set minimum OS versions.
Swift Property Wrappers: Reusable Logic for Properties
A Swift Property Wrapper is a template for a property's get/set logic, letting you reuse behaviors like data validation without boilerplate. It powers SwiftUI's @State and is great for managing UserDefaults.
Refining Objective-C APIs for Swift with NS_SWIFT_NAME
NS_SWIFT_NAME gives Objective-C code a Swifty alias without changing the original API. Use it to modernize legacy frameworks by removing prefixes for Swift consumption.
Optimizing iOS App Launch Time
An app launch is a race against the system's watchdog timer. The OS loads your app in two phases: pre-main and main. Optimizing both is key. The biggest footgun is ignoring the pre-main phase, where bloated dynamic libraries can kill performance.
Swift Errors and the NSError Bridge
Swift's modern `Error` protocol and Objective-C's `NSError` are different, but Swift automatically "bridges" them. This is key when calling older Apple APIs, which throw `NSError`s.
Lightweight Generics: Type Safety for Objective-C
Lightweight generics bolt type safety onto Objective-C collections like NSArray. This lets Swift see an NSArray<NSString *> as a type-safe [String] instead of [Any], preventing runtime crashes.
Objective-C Nullability: Bridging to Swift's Optionals
Nullability annotations are like adding `?` or `!` to your Objective-C pointers, telling Swift how to handle `nil`. They're essential in mixed codebases to bridge Objective-C's pointers to Swift's safe Optionals.
Autorelease Pools: Managing Temporary Object Memory
Think of an autorelease pool as a temporary holding pen for objects. It defers their deallocation until a code block ends. UI frameworks handle this, but you'll add one in tight loops with many temporary objects to reduce peak memory, or on background threads.
Selectors: Objective-C's Function Pointers in Swift
A selector is a lightweight name for a method, not a direct pointer. Swift uses it to call Objective-C code dynamically at runtime. It's common in UIKit for target-action patterns, like button taps. The footgun: forgetting `@objc` causes a runtime crash.
Bridging Header: Mixing Objective-C and Swift
A bridging header is a translation list for Xcode, letting your new Swift code call old Objective-C code. It's essential when migrating a legacy codebase or using an Objective-C library. The footgun: it only exposes Objective-C to Swift, not vice-versa.
Main Thread Checker: Keep Your UI Responsive
The Main Thread Checker is an Xcode tool that catches UI updates on background threads, which cause crashes or glitches. It runs during debugging, flagging AppKit, UIKit, or SwiftUI calls made off the main thread.
Grand Central Dispatch (GCD): Your App's Task Manager
Grand Central Dispatch (GCD) is your app's task manager, letting you run code in the background without freezing the UI. Use it to offload network requests or heavy computations, then update the UI on the main queue.
Swift Library Evolution: Shipping Break-Proof Frameworks
Library Evolution lets you update a Swift framework without breaking apps that use it, even if they don't recompile. It's crucial for shipping binary SDKs. The footgun is that many seemingly safe changes, like adding a struct property, are binary-breaking by…
Swift Package Resources: Bundling Assets with Code
A Swift Package resource bundle is like a fanny pack for your code, letting you package assets like images and data files directly with your library. This makes your package self-contained.
Carthage: A Decentralized iOS Dependency Manager
Carthage is a dependency manager that prioritizes developer control. It compiles dependencies into binary frameworks but leaves final integration to you, avoiding automatic changes to your Xcode project.
Xcode Workspaces: Managing Multiple Related Projects
An Xcode Workspace is a container for multiple related projects, letting them share code and resources. Use it when building an app with its own frameworks or when managing dependencies like CocoaPods.
Get Swift bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.