skip to content

Design Goals and History

Swift replaced Objective-C as Apple's main language by promising safety without losing speed, and it evolves through public proposals. Interviewers open with why Swift and how it differs.

on this pageshow

explore

questions

4

What do Swift's design goals of safety, speed and expressiveness mean in practice, with one concrete language rule behind each?

level: juniorimportance: should knowfreq 50%

answer

  1. whole classes of errors defined away
  2. initialized before use, nil made explicit
  3. stop rather than wrap or overrun
  4. the obvious code should also be fast
  5. inference, closures, pattern matching

basics

~20 s

Safe means Swift defines away common bugs: values are initialized before use, nil must be handled through Optional, and overflow or out-of-bounds indexing traps. Fast means optimized native code. Expressive means type inference, closures and pattern matching.

solid answer

~50 s

Swift's **safety** goal is that whole classes of errors are defined away. The compiler rejects reading a `let` or `var` before every path has assigned it, `Optional` makes a possibly missing value a different type from a present one, and at run time integer overflow and an out-of-bounds array index trap instead of wrapping silently or reading past the storage; memory is managed automatically with ARC. **Speed** means Swift compiles ahead of time to optimized native code, and the standard library is shaped so that the obvious code is also the fast code: value types such as `Array` and `String`, and generics the optimizer can specialize. **Expressiveness** means a light syntax with type inference, closures, enums with associated values and pattern matching. Each safety check has a named opt-out you write on purpose, such as `&+` for wrapping arithmetic or the `Unsafe` pointer types.

code

swift · 15 lines
swift
let scores = [90, 72, 85]

// Optional: the compiler makes you handle a missing value.
let first: Int? = scores.first
if let first {
    print("first score:", first)
}

// Overflow traps instead of wrapping silently; &+ opts into wrapping.
let big = Int8.max
print(big &+ 1)        // -128, wrapping on purpose

// Bounds are checked at run time: this line traps.
let index = scores.count
print(scores[index])   // Fatal error: Index out of range

go deeper

for a junior

Name one rule per goal: initialized before use, Optional for nil, checked overflow and bounds for safety; compiled native code for speed; inference and closures for expressiveness.

for a middle

Separate the compile-time rules (definite initialization, Optional) from the run-time traps (bounds, overflow), and name the explicit opt-outs such as the &+ operator.

for a senior

Explain the trade-off behind traps: a deliberate stop protects data, recoverable failures belong in throws, and unsafe APIs are opt-ins a reviewer can search for.

for a principal

Treat the goals as defaults a codebase builds on: every Unsafe name, force unwrap and wrapping operator is a visible exception that a review can ask to justify.

## Where the three goals come from The opening chapter of *The Swift Programming Language*, "About Swift", states the aims directly: Swift is "a safe and fast programming language", it should be "as expressive and enjoyable as a scripting language", and "the compiler is optimized for performance and the language is optimized for development, without compromising on either." Apple introduced Swift in 2014 as the successor to Objective-C on its platforms and open-sourced it in December 2015; the same goals now apply wherever Swift runs, from phones to Linux servers. Interviewers often open with "why Swift?", and a strong answer names a **concrete rule** for each goal instead of repeating the three adjectives. ## Safe: errors defined away by default The book lists the rules that make Swift **safe by default**: - **Definite initialization.** Variables are always initialized before use. A `let` assigned on only one branch of an `if` and then returned is a compile error: `constant 'grade' used before being initialized`. - **Bounds checking.** Array indices are checked. Reading `scores[3]` from a three-element array stops the program; a debug build prints `Fatal error: Index out of range`. - **Overflow checking.** Integer arithmetic is checked. `Int8.max + 1` written as a constant expression is rejected at compile time, and the same overflow computed at run time traps. Wrapping is opt-in through `&+`, `&-` and `&*`. - **Explicit nil.** `Optional` makes absence part of the type: a `String` can never be `nil`, and a `String?` has to be unwrapped before use. - **Automatic memory management.** Class instances are managed by **Automatic Reference Counting (ARC)**, so ordinary code never calls retain or release by hand. - **Error handling.** `throws`, `try` and `do`-`catch` give controlled recovery from failures a caller can expect. A **trap** is a deliberate, immediate stop of the process at the first invalid state. That is the design choice behind "safe": a programmer error halts the process deterministically at the faulting line, leaving a crash report that points there, instead of continuing with silently wrong or corrupted data that surfaces far from the cause. Failures the caller can recover from, such as a missing file, use `throws` instead. Safety is a default, not an absolute. The `UnsafePointer` family, `unowned(unsafe)` and the wrapping operators exist, and each carries a name that marks the opt-out at the call site where a reviewer can see it. Compile-time checking of data races is the newest layer: the Swift 6 language mode turns those diagnostics into errors. ## Fast: the obvious code should perform best Swift code is compiled ahead of time to native machine code and optimized for the hardware. The book's guiding principle is that "the obvious way to write your code should also perform the best". In practice: 1. Core types such as `Int`, `Double`, `String`, `Array` and `Dictionary` are **value types** (structs), so a copy is an independent value that needs no locking or defensive copying. 2. Calls on structs, enums and `final` classes can be dispatched directly and inlined, and generic code can be **specialized** for the concrete type it is called with. 3. The safety checks stay on in `-O` optimized builds too: a bad index or an overflow still traps there, only without the message, so speed is not bought by switching safety off. How dispatch and specialization actually work is the subject of the lessons on method calls and compiler optimization. ## Expressive: say more with less - **Type inference**: `let names = ["Ana", "Bo"]` is a `[String]` without an annotation. - **Closures** with trailing syntax and shorthand arguments: `names.map { $0.count }`. - **Enums with associated values** and **pattern matching** model states directly. - **Generics and protocols** let one algorithm serve many types without casts. ## The goals at a glance | Goal | A rule that delivers it | When it acts | |---|---|---| | Safe | definite initialization, `Optional` | compile time | | Safe | bounds and overflow checks | run time (trap) | | Fast | native code, value types, specialization | compile time | | Expressive | inference, closures, pattern matching | compile time | ## What a strong interview answer adds Pick one rule per goal and name its trade-off: a trap is a crash by design, chosen over undefined behaviour; recoverable errors belong in `throws`; and every opt-out (`&+`, `!`, `Unsafe…`) is spelled so that it stands out in review.

  • If Swift traps on overflow and on a bad array index, in what sense is a crash safe?
    A trap stops the process at the first invalid state, deterministically, and the crash report points at the faulting line. Continuing would mean a wrapped value or a read past an array's storage that corrupts data and surfaces far from the cause. Swift treats these as programmer errors to fix; failures a caller can recover from use `throws`, and wrapping arithmetic is an explicit opt-in with `&+`, `&-` and `&*`.
  • Is every line of Swift memory-safe by construction?
    No. Swift is safe by default and lets you opt out under names that say so: the `UnsafePointer` and `UnsafeRawBufferPointer` family, `unowned(unsafe)`, and C APIs called through interop. Those opt-outs are greppable, which is the point: the unsafe code is small, named and reviewable instead of spread through every line.

  • Swift frees memory with a tracing garbage collector.
  • Integer overflow in Swift wraps around silently.
  • A non-optional Swift String can still be nil at run time.
  • Safe means a Swift program can never crash.
  • Swift reads an unassigned local constant as zero or empty.
open as a page

When a team picks Swift instead of Objective-C for a new iOS module, which language differences matter most, and what does mixing the two cost?

level: middleimportance: should knowfreq 55%

basics

~20 s

Swift makes absence a type (Optional), gives String, Array and Dictionary value semantics, has full generics, calls struct methods directly and reports errors with throws. Objective-C sees only Swift declarations representable in Objective-C, so Swift-only types stay hidden from it.

open as a page

A binary Swift framework built with an older compiler still loads and compiles in an app built with Swift 6.4; what do ABI stability and module stability each guarantee?

level: seniorimportance: should knowfreq 30%

basics

~20 s

ABI stability (Swift 5.0, Apple platforms) lets binaries from different Swift compilers link and run together. Module stability (Swift 5.1) lets a newer compiler import the framework through its textual .swiftinterface file, which needs library evolution enabled.

open as a page

How does a change reach the Swift language through the Swift Evolution process, and who makes the final decision on a proposal?

level: middleimportance: nice to knowfreq 25%

basics

~20 s

An idea is pitched on the Swift Forums, written up as a numbered SE proposal, usually with an implementation, publicly reviewed, and decided by the Language Steering Group; the proposal's Status line then records the Swift release that shipped it.

open as a page